办法从命令行或批处理文件运行Excel宏? [英] Way to run Excel macros from command line or batch file?
问题描述
我有一个Excel VBA宏,我需要从一个批处理文件访问文件时运行,但不是每一个我打开它(因此不使用打开文件的事件)的时间。有没有办法运行在命令行或批处理文件中的宏?我不熟悉这样的命令。
I have an Excel VBA macro which I need to run when accessing the file from a batch file, but not every time I open it (hence not using the open file event). Is there a way to run the macro from the command line or batch file? I'm not familiar with such a command.
假设Windows NT环境。
Assume a Windows NT environment.
推荐答案
您可以启动Excel,打开工作簿和运行一个VBScript文件中的宏。
You can launch Excel, open the workbook and run the macro from a VBScript file.
复制code以下到记事本中。
Copy the code below into Notepad.
更新'的 MyWorkbook.xls 的'和'的 MyMacro 的'参数。
Update the 'MyWorkbook.xls' and 'MyMacro' parameters.
用VBS扩展名保存并运行它。
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", 0, True)
xlApp.Run "MyMacro"
xlApp.Quit
Set xlBook = Nothing
Set xlApp = Nothing
End Sub
运行宏中的关键行是:
The key line that runs the macro is:
xlApp.RunMyMacro
这篇关于办法从命令行或批处理文件运行Excel宏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!