title: Run Excel Macro from Outside Excel Using VBScript From Command Line
body: <p>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</p>
  
  <pre><code>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
  </code></pre>
  
  <p>And here is the Macro I'm trying to access:</p>
  
  <pre><code>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\" &amp; "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
  </code></pre>
  
  <p>I tried the solutions located at <a href="https://stackoverflow.com/questions/3183286/is-it-possible-to-run-a-macro-in-excel-from-external-command">Is it possible to run a macro in Excel from external command?</a> 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.</p>
  
  <p>EDIT: Excel 2003.</p>