ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   C# and Excel: how to get reference to Excel VBA form (https://www.excelbanter.com/excel-programming/419897-c-excel-how-get-reference-excel-vba-form.html)

griza

C# and Excel: how to get reference to Excel VBA form
 
Hi,

I am trying to automate work with Excel file that uses macroses and VBA
controls and forms. In particular, Excel file has the button that invokes a
new form when clicking on it.

I can successfully access and get reference to the button, but fails to do
this for the form

My C# code:

....
using Excel = Microsoft.Office.Interop.Excel
using MSForm = Microsoft.Vbe.Interop.Forms;

// start Excel and open the file
Excel.Application oXL = new Excel.Application();
oXL.Visible = true;
Excel.Workbook oWb = (Excel.Workbook)(oXL.Workbooks.Open(file,...));
Excel.Worksheet oSheet = (Excel.Worksheet)oWB.ActiveSheet;

// get reference to the button by its VBA name - works successfully
Excel.OLEObject obj1 = (Excel.OLEObject)(oSheet.OLEObjects("myButton"));
MSForm.CommandButton requestButton = (MSForm.CommandButton)(obj1.Object);

// emulate clicking the button and wait a little until the form appears
....

// trying to get reference to the form that has appeared by its VBA name -
get exception 0x800A03EC
object myForm = oSheet.OLEObjects("myForm");


Question: How can I refer Excel's user form from my C# code?


griza

C# and Excel: how to get reference to Excel VBA form
 
I've got answer in another thread:

http://social.msdn.microsoft.com/For...8-a01ceda3573f

Thanks!



"griza" wrote:

Hi,

I am trying to automate work with Excel file that uses macroses and VBA
controls and forms. In particular, Excel file has the button that invokes a
new form when clicking on it.

I can successfully access and get reference to the button, but fails to do
this for the form

My C# code:

...
using Excel = Microsoft.Office.Interop.Excel
using MSForm = Microsoft.Vbe.Interop.Forms;

// start Excel and open the file
Excel.Application oXL = new Excel.Application();
oXL.Visible = true;
Excel.Workbook oWb = (Excel.Workbook)(oXL.Workbooks.Open(file,...));
Excel.Worksheet oSheet = (Excel.Worksheet)oWB.ActiveSheet;

// get reference to the button by its VBA name - works successfully
Excel.OLEObject obj1 = (Excel.OLEObject)(oSheet.OLEObjects("myButton"));
MSForm.CommandButton requestButton = (MSForm.CommandButton)(obj1.Object);

// emulate clicking the button and wait a little until the form appears
...

// trying to get reference to the form that has appeared by its VBA name -
get exception 0x800A03EC
object myForm = oSheet.OLEObjects("myForm");


Question: How can I refer Excel's user form from my C# code?



All times are GMT +1. The time now is 05:10 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com