我試圖從Excel文件以外運行Excel宏。我目前使用從命令行運行的「.vbs」文件,但它一直告訴我無法找到宏。下面是我嘗試使用從外部Excel使用VBScript從命令行運行Excel宏
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
我試過位於Is it possible to run a macro in Excel from external command?的解決方案遠遠得到這個(和改性當然),但它似乎沒有工作。我不斷收到錯誤「Microsoft Office Excel:無法找到宏'Macro.TestMacro'。
編輯:Excel 2003.
你有沒有嘗試刪除 「宏」。和/或括號,只是把它叫做這個objExcel.Application.Run「TestMacro」? – deusxmach1na 2012-04-19 16:10:40
objExcel.Application.Run「test.xls!Macro.TestMacro」 – 2012-04-19 16:15:55
這兩個都不工作:( – muttley91 2012-04-19 16:19:58