vba 是否可以从外部命令在 Excel 中运行宏?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3183286/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 11:50:08  来源:igfitidea点击:

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

vbaexcel-vbaexcel

提问by Jason

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?

假设我想在一个外部程序中编写一个 VBA 代码,该程序打开一个 Excel 文件,运行一个宏,保存(并对任何弹出窗口说是),然后关闭 Excel。有可能这样做吗?如果是这样,我将如何实施它?

回答by Robert Mearns

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

您可以启动 Excel,打开工作簿,然后从 VBScript 文件操作工作簿。

Copy the code below into Notepad.

将下面的代码复制到记事本中。

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

更新“MyWorkbook.xls”和“Sheet1”参数。

Save it with a vbs extension and run it.

使用 vbs 扩展名保存并运行它。

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.

上面的代码启动 Excel 打开一个工作簿,在单元格 A1 中输入一个值,使其加粗并更改单元格的颜色。然后保存并关闭工作簿。然后关闭 Excel。

回答by Marcus from London

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).

根据您要实现的目标,您还可以通过来自其他应用程序(例如 Access 或 Word)的(OLE)自动化或您自己在其他环境(例如 Visual Basic (6) 中编写的应用程序)中“控制”Excel。也可以使用您选择的语言通过 .Net 实现这一点(尽管有些语言比其他语言更容易实现)。

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

您是想从外部应用程序控制 Excel 还是只是从外部触发现有工作簿中的 VBA 宏?

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).

同样,根据您的意图,工作簿可以有一个 Auto Open 宏,它可以根据外部值(比如 ini 文件或数据库值)有条件地运行。

回答by ASH

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.

现在,这不是“外部命令”,但 Windows 任务计划程序会很好地为您打开该文件,一旦打开,您就可以运行如下所示的小脚本。

Private Sub Workbook_Open()
   Call CommandButton1_Click
End Sub

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

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