我正在尝试从 Excel 文件外部运行 Excel 宏。我目前正在使用从命令行运行的“.vbs”文件,但它一直告诉我找不到宏。这是我正在尝试使用的脚本
Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open("test.xls")
objExcel.Application.Visible = True
objExcel.Workbooks.Add
objExcel.Cells(1, 1).Value = "Test value"
objExcel.Application.Run "Macro.TestMacro()"
objExcel.ActiveWorkbook.Close
objExcel.Application.Quit
WScript.Echo "Finished."
WScript.Quit
这是我试图访问的宏:
Sub TestMacro()
'first set a string which contains the path to the file you want to create.
'this example creates one and stores it in the root directory
MyFile = "C:\Users\username\Desktop\" & "TestResult.txt"
'set and open file for output
fnum = FreeFile()
Open MyFile For Output As fnum
'write project info and then a blank line. Note the comma is required
Write #fnum, "I wrote this"
Write #fnum,
'use Print when you want the string without quotation marks
Print #fnum, "I printed this"
Close #fnum
End Sub
我尝试了位于是否可以从外部命令在 Excel 中运行宏的解决方案?走到这一步(当然还有修改),但它似乎没有用。我不断收到错误“Microsoft Office Excel:找不到宏“Macro.TestMacro”。
编辑:Excel 2003。