11

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.

Luuklag
  • 3,897
  • 11
  • 38
  • 57
Mehdi
  • 1,494
  • 5
  • 33
  • 53

4 Answers4

14

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.

Govert
  • 16,387
  • 4
  • 60
  • 70
  • Thanks, but i'm not sure that the users have PowerShell installed ... I finally used a VB Script and launch it from command line – Mehdi Jun 05 '12 at 08:43
10

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 
Mehdi
  • 1,494
  • 5
  • 33
  • 53
  • Note that while you can set the `Saved` property, it does not save the file. Use the `Save` method instead. – Rachel Hettinger Aug 18 '14 at 19:13
  • Did you figure out how to generalize this for any code? – Lime Aug 21 '20 at 03:59
  • Nope @Lime I didn't – Mehdi Sep 08 '20 at 08:54
  • Thank you! Mehdi your answer worked for me too with a small adjustment. The line `xl.Application.Visible = True` was leaving open a zombie/phantom EXCEL.EXE process using up memory (found it through Windows Task Manager). Using `xl.Application.Visible = False` instead seems to eliminate the zombie. – Eats Sep 13 '17 at 00:52
0

AutoIt also offers great COM support and has a lot of built-in Excel-controlling functions. You can compile the script to an .EXE and then run it from the command line.

sigil
  • 9,370
  • 40
  • 119
  • 199
0

If you would prefer to code in C# use this template

void Main()
{
    var oExcelApp = (Microsoft.Office.Interop.Excel.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application");
    try{
        //oExcelApp.Visible = true;
        var WB = oExcelApp.ActiveWorkbook;
        var WS = (Worksheet)WB.ActiveSheet;
        ((string)((Range)WS.Cells[1,1]).Value).Dump("Cell Value");
        oExcelApp.Run("test").Dump("macro");
    }
    finally{
        if(oExcelApp != null)
            System.Runtime.InteropServices.Marshal.ReleaseComObject(oExcelApp);
        oExcelApp = null;
   }
}
t3dodson
  • 3,949
  • 2
  • 29
  • 40
  • It was unfortunately not possible to use C# ! The only possible option was Sheel Scripting ... Thanks anyway ! but i can't test your solution ... – Mehdi Dec 10 '14 at 16:18
  • not a problem, I saw this question was old. I was looking for this functionality and I wanted to give a way to do it in C# for others who may find it useful in the future. – t3dodson Dec 11 '14 at 00:48