简体   繁体   中英

Is it possible to run a macro in Excel from external command?

Let's say I want to program a VBA code in an external program that opens an Excel file, runs a macro, saves (and say yes to any pop up windows), and close Excel. Is it possible to do so? If so, how would I go about implementing it?

You can launch Excel, open a workbook and then manipulate the workbook from a VBScript file.

Copy the code below into Notepad.

Update the 'MyWorkbook.xls' and 'Sheet1' parameters.

Save it with a vbs extension and run it.

Option Explicit

On Error Resume Next

ExcelMacroExample

Sub ExcelMacroExample() 

  Dim xlApp 
  Dim xlBook 

  Set xlApp = CreateObject("Excel.Application") 
  Set xlBook = xlApp.Workbooks.Open("C:\MyWorkbook.xls") 
  xlBook.Sheets("Sheet1").Cells(1, 1).Value = "My text"
  xlBook.Sheets("Sheet1").Cells(1, 1).Font.Bold = TRUE
  xlBook.Sheets("Sheet1").Cells(1, 1).Interior.ColorIndex = 6
  xlBook.Save
  xlBook.Close
  xlApp.Quit 

  Set xlBook = Nothing 
  Set xlApp = Nothing 

End Sub 

This code above launches Excel opens a workbook, enters a value in cell A1, makes it bold and changes the colour of the cell. The workbook is then saved and closed. Excel is then closed.

Depending on what you are trying to achieve you may also 'control' Excel via (OLE) Automation from another application such as Access or Word or from your own application written in another environment such as Visual Basic (6). It is also possible to achieve this via .Net using a language of your choice (although some are easier to implement than others).

Are you wanting to control Excel from an external application or simply trigger a VBA macro in an existing workbook from the outside?

Again, depending on your intention, the workbook could have an Auto Open macro which could be conditional run based on an external value (say an ini file or database value).

I can think of several ways to do this.

You can start excel by creating a file with NotePad or a similar text editor.

Here are some steps:

    Launch NotePad
    Add the following line of text. Replace test.xlsm with the name and path for your file:
    start Excel.exe "C\test.xlsm"
    Save the file as "Test.bat". 
    Run the batch file.
    The batch file should launch Excel and then open your file. The code in your workbook should run

OR

Again, using Notepad.

Option Explicit

On Error Resume Next

ExcelMacroExample

Sub ExcelMacroExample() 

  Dim xlApp 
  Dim xlBook 

  Set xlApp = CreateObject("Excel.Application") 
  Set xlBook = xlApp.Workbooks.Open("C:\MyWorkbook.xls", 0, True) 
  xlApp.Run "MyMacro"
  xlApp.Quit 

  Set xlBook = Nothing 
  Set xlApp = Nothing 

End Sub

Or, this.

'Code should be placed in a .vbs file
Set objExcel = CreateObject("Excel.Application")
objExcel.Application.Run "'C:\Users\Ryan\Desktop\Sales.xlsm'!SalesModule.SalesTotal"
objExcel.DisplayAlerts = False
objExcel.Application.Quit
Set objExcel = Nothing

Now, this isn't 'an external command', but the Windows Task Scheduler will do a nice job of opening that file for you, and once it is opened, you can run a tiny script like the one you see below.

Private Sub Workbook_Open()
   Call CommandButton1_Click
End Sub

https://www.sevenforums.com/tutorials/11949-elevated-program-shortcut-without-uac-prompt-create.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM