开发者

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

开发者 https://www.devze.com 2023-01-06 00:19 出处:网络
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 w

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号