How to Launch an Excel macro from command line (Without Worksheet_Open Event)?

后端 未结 5 1246
走了就别回头了
走了就别回头了 2020-12-14 12:17

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

5条回答
  •  旧巷少年郎
    2020-12-14 12:57

    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 
    

提交回复
热议问题