PC Review


Reply
Thread Tools Rate Thread

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

 
 
griza
Guest
Posts: n/a
 
      12th Nov 2008
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?

 
Reply With Quote
 
 
 
 
griza
Guest
Posts: n/a
 
      13th Nov 2008
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?
>

 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I reference a Worksheet from a Form (Add-in for Excel 2003) royend Microsoft Excel Programming 3 17th Mar 2009 10:20 AM
Re: How to enter an Excel cell reference into a line of text in Excel Nick Hodge Microsoft Excel Worksheet Functions 0 5th Dec 2006 08:28 PM
Reference to Excel worksheet from Outlook form Long Nguyen Microsoft Outlook Form Programming 2 4th Feb 2005 04:07 AM
Reference Access created form in excel =?Utf-8?B?UnlhbiBQYXRjaA==?= Microsoft Excel Programming 0 31st Jan 2004 08:01 PM
How to I reference an Excel Module form MS Access? June Macleod Microsoft Access 2 17th Dec 2003 10:56 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:21 AM.