Skip to: Site menu | Main content

Login

Name: 
Password:
Remember me?
Register

Running an Excel Macro from Outside Excel

written by Mark Rowlinson - Last updated Nov 2004

If you have a macro in an Excel spreadsheet that you are automating from VB6, Word etc. then you can run the macro in the workbook using the Run method of the application object.

You pass it a string value of the macro name followed by any arguments you wish to pass it. The macro name can specify the workbook name too followed by an exclamation mark before the actual macro name. It is best to enclose the workbook name in ' marks as if the workbook name has spaces it will cause problesm otherwise.

For exmaple suppose we have the following macro in a workbook called Test.xls:

 
Sub xlmacro(s1 As String, s2 As String) 
     
    MsgBox  s1 & " " & s2 
     
End Sub 
 

We could call this macro to display the message 'Hello World' using the following code (it is assumed that your application object is called xlApp):

 
Sub test() 
     
    xlApp.Run Macro:="'Test.xls'!xlmacro", Arg1:="Hello", Arg2:="World" 
     
End Sub