Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run Excel Macro from Outside Excel Using VBScript From Command Line

I'm trying to run an Excel macro from outside of the Excel file. I'm currently using a ".vbs" file run from the command line, but it keeps telling me the macro can't be found. Here is the script I'm trying to use

Set objExcel = CreateObject("Excel.Application") Set objWorkbook = objExcel.Workbooks.Open("test.xls")  objExcel.Application.Visible = True objExcel.Workbooks.Add objExcel.Cells(1, 1).Value = "Test value"  objExcel.Application.Run "Macro.TestMacro()" objExcel.ActiveWorkbook.Close   objExcel.Application.Quit WScript.Echo "Finished." WScript.Quit 

And here is the Macro I'm trying to access:

Sub TestMacro() 'first set a string which contains the path to the file you want to create. 'this example creates one and stores it in the root directory MyFile = "C:\Users\username\Desktop\" & "TestResult.txt" 'set and open file for output fnum = FreeFile() Open MyFile For Output As fnum 'write project info and then a blank line. Note the comma is required Write #fnum, "I wrote this" Write #fnum, 'use Print when you want the string without quotation marks Print #fnum, "I printed this" Close #fnum End Sub 

I tried the solutions located at Is it possible to run a macro in Excel from external command? to get this far (and modified of course) but it didn't seem to work. I keep getting the error `Microsoft Office Excel: The macro 'Macro.TestMacro' cannot be found.

EDIT: Excel 2003.

like image 555
muttley91 Avatar asked Apr 19 '12 16:04

muttley91


People also ask

Can you run a macro outside of Excel?

There are many instances where you could copy your Excel Macro to a VBScript file and run it directly, but sometimes you don't want to deal with that headache. It's in these moments where it's beneficial to create a VBScript file to run your existing macros from outside Excel. You already have a working VBA macro.

How do I run VBA code without opening Excel?

You can't run a Excel VBA Macro without opening the File that contains the macro. If you want you can launch the excel application in hidden mode and then run the macro after opening the file in hidden mode from a VBS file.

How do I run an Excel macro command in Automation Anywhere?

You can store your Macro. xlsxm file to AA My Docs folder and pass your excel file name (on which you want to perform macro operation) and path to your macro. You need to configure your Macros file and as per the requirement tool has to pass the file path and name as parameter in Run excel macros command.


1 Answers

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.

like image 177
Wolf-Kun Avatar answered Oct 17 '22 10:10

Wolf-Kun