Run Excel Macro from Outside Excel Using VBScript From Command Line
Ok, it's actually simple. Assuming that your macro is in a module,not in one of the sheets, you use:
objExcel.Application.Run "test.xls!dog" 'notice the format of 'workbook name'!macro
For a filename with spaces, encase the filename with quotes.
If you've placed the macro under a sheet, say sheet1, just assume sheet1 owns the function, which it does.
objExcel.Application.Run "'test 2.xls'!sheet1.dog"
Notice: You don't need the macro.testfunction notation you've been using.
I think you are trying to do this? (TRIED AND TESTED)
This code will open the file Test.xls and run the macro TestMacro
which will in turn write to the text file TestResult.txt
Option ExplicitDim xlApp, xlBookSet xlApp = CreateObject("Excel.Application")'~~> Change Path hereSet xlBook = xlApp.Workbooks.Open("C:\Test.xls", 0, True)xlApp.Run "TestMacro"xlBook.ClosexlApp.QuitSet xlBook = NothingSet xlApp = NothingWScript.Echo "Finished."WScript.Quit
Since my related question was removed by a righteous hand after I had killed the whole day searching how to beat the "macro not found or disabled" error, posting here the only syntax that worked for me (application.run didn't, no matter what I tried)
Set objExcel = CreateObject("Excel.Application")' Didn't run this way from the Modules'objExcel.Application.Run "c:\app\Book1.xlsm!Sub1"' Didn't run this way either from the Sheet'objExcel.Application.Run "c:\app\Book1.xlsm!Sheet1.Sub1"' Nor did it run from a named Sheet'objExcel.Application.Run "c:\app\Book1.xlsm!Named_Sheet.Sub1"' Only ran like this (from the Module1)Set objWorkbook = objExcel.Workbooks.Open("c:\app\Book1.xlsm")objExcel.Run "Sub1"
Excel 2010, Win 7