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

G

griza

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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top