我正在尝试从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'。
米琪卡哇伊