View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.programming
Steve Yandl Steve Yandl is offline
external usenet poster
 
Posts: 284
Default execute macro from command line

I'm not sure that you can do this directly but you can use a vbs file as an
intermediary and that might accomplish what you want.

For the example, I have a macro named "macroA" that is in the workbook
"C:\Test\Bookx.xls". I then use notepad to create a file named
"C:\Test\LaunchMacro.vbs" (the vbs file would not have to be in the same
folder, but you will need to know the path for your command line entry).
The contents of LaunchMacro.vbs are shown between the lines below:

______________________________________

myMacro = WScript.Arguments.Item(0)

Set objXL = CreateObject("Excel.Application")
Set objWkbk = objXL.Workbooks.Open("C:\Test\Bookx.xls")
objXL.Run myMacro

objWkbk.Save
objWkbk.Close
objXL.Quit

_________________________________________

Now, if I launch the vbs file and hand it the name of the macro as the
argument, the workbook will open hidden, run the macro, save changes and
close. The command line in this case would be:

C:\Test\LaunchMacro.vbs "macroA"


Steve




"swansong" wrote in message
...
Hello,

I would like to run a macro in a workbook from the command line. The macro
modifies the contents of the workbook. This is so I can automate a task
rather than open the workbook, run macro, and save.

Thanks!