Thursday, July 5, 2007

Execute an Office Macro from a .NET Application

I have a pretty extensive macro written in Excel VBA.  I had a need to call this macro from a .NET application.  Before I executed the VBA macro, I needed to set a couple of combobox controls located on the worksheet.
 
Most of the solutions found on the Internet referred to using VSTO (Visual Studio Tools for Office).  One article I came across (http://support.microsoft.com/kb/306683) explained how to setup a .NET procedure called RunMacro that would execute a VBA macro stored in an Office application.  The article demonstrates that this same RunMacro procedure could be used to call an Excel Macro or Word, Accesss, or PowerPoint.
 
My solution was to bring the RunMacro code from the above article into my .NET application.  Next, in my Excel VBA code, I added a proxy procedure that would receive arguments used to set the combobox controls and then execute the macro.
 
My proxy procedure in VBA Code was similar to:
Public Sub MyProxyProcedure(MyArgOne As String, MyArgTwo As String)
    Sheets("Sheet1").cboOne.Value = MyArgOne
    Sheets("Sheet1").cboTwo.Value = MyArgTwo
    Call MyVBAProcedure
End Sub
 
Public Sub MyVBAProcedure
    'DO SOMETHING HERE
End Sub
 
My .NET Code to execute the VBA macro was similar to:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

' requires .net reference to Microsoft.Office.Interop.Excel
DIM oExcel as New Microsoft.Office.Interop.Excel.Application
DIM oWB as Microsoft.Office.Interop.Excel.Workbook
oWB = oExcel.Workbooks.Open("c:\MyExcelWorkbook.xls")
RunMacro(oExcel, New Object() {"MyProxyProcedure", _
   strArgOne, strArgTwo})

End Sub

Private Sub RunMacro(ByVal oApp As Object, _
                                 ByVal RunArgs As Object())
  oApp.GetType().InvokeMember("Run", _
    Reflection.BindingFlags.Default Or _
    Reflection.BindingFlags.InvokeMethod, _
    Nothing, oApp, oRunArgs)

End Sub

 
Now I am able to make use of the VBA Code I have invested and integrate into new .NET applications.

6 comments:

Anonymous said...

viagra generic soft tab viagra price videos viagra viagra in the water viagra logo viagra liver damage effects of viagra on women homemade viagra viagra suppliers uk alternative viagra viagra herb alternative viagra rrp australia cost viagra cheap buy online viagra cheap buy online

Anonymous said...

blog.chasecomputerservices.com is very informative. The article is very professionally written. I enjoy reading blog.chasecomputerservices.com every day.
instant payday
canada payday loans

Anonymous said...

Nice post! GA is also my biggest earning. However, it’s not a much.

rH3uYcBX

Anonymous said...

That’s Too nice, when it comes in india hope it can make a Rocking place for youngster.. hope that come true.

rH3uYcBX

Anonymous said...

My computer keep on hanging and then restart when I open any games. I scan it with mcafee anti virus, and my computer has no virus, scan it with superantispyware and no threats found. I also used kaspersky rescue disk, and nothings found. I format my computer and install a games, and it hangs again. By the way, I think the games is not the problem, it is on my computer but I don't know why its hanging and restarting now. Help me pls. THnx, Happy new year to all. [url=http://gordoarsnaui.com]santoramaa[/url]

Anonymous said...

What is captcha code?, pls provide me captcha code codes or plugin, Thanks in advance.

 
2007, Chase Computer Services.