Is it possible to launch an Excel Macro from command line?
I don't want to use the Worksheet_Open
event and just open the Excel File.
I need to launch specific macro that exists in the Excel WorkBook.
Place a cursor inside the procedure and press F5 to run it. The macro should run perfectly without any runtime errors if we have to run it successfully from the command line.
Opening Without Automatic MacrosOpen Excel, go to the File menu, click "Open" and locate your file. Hold down the "Shift" key while you click "Open," and continue holding it until the workbook finishes loading.
Pressing F8 will let you step through the macro code one line at a time. Edit - This will open the Visual Basic Editor and let you edit the macro code as needed. Once you've made changes, you can press F5 to run the macro from the editor.
Use the Windows PowerShell, it has excellent COM interop support.
I have the workbook c:\TestBeep.xlsm with a macro called "Test". This is my transcript:
PS C:\> $app = New-Object -comobject Excel.Application
PS C:\> $wb = $app.Workbooks.Open("c:\TestBeep.xlsm")
PS C:\> $wb.Name
TestBeep.xlsm
PS C:\> $app.Run("Test")
PS C:\> $app.Quit()
Optionally you can add in $app.Visible = $True
to make the window visible.
I finally created a VB Script and launched it from the command line:
Option Explicit
LaunchMacro
Sub LaunchMacro()
Dim xl
Dim xlBook
Dim sCurPath
sCurPath = CreateObject("Scripting.FileSystemObject").GetAbsolutePathName(".")
Set xl = CreateObject("Excel.application")
Set xlBook = xl.Workbooks.Open(sCurPath & "\MyWorkBook.xlsm", 0, True)
xl.Application.Visible = True
xl.Application.run "MyWorkBook.xlsm!MyModule.MyMacro"
xl.DisplayAlerts = False
xlBook.saved = True
xl.activewindow.close
xl.Quit
Set xlBook = Nothing
Set xl = Nothing
End Sub
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With