1
我有一個包含宏的excel文件。該宏是在開發人員選項卡的ThisWorkbook中編寫的。我想通過在Windows任務計劃程序中安排它來自動運行宏。如何成功執行vbscript?
我做了一些研究,發現我必須爲此編寫一個vbscript並運行vb腳本才能運行宏。
這是vb腳本。應該VBScript的:
- 打開Excel文件
- 運行宏
- 關閉Excel文件
這應該會自動在預定的時間內完成,每天通過使用Windows任務調度。
到目前爲止,這是VB腳本:
'Script to start my filter.xls program with named macro
'MsgBox "In RunExcel"
' Create an Excel instance
Dim myExcelWorker
Set myExcelWorker = CreateObject("Excel.Application")
' Disable Excel UI elements
myExcelWorker.DisplayAlerts = False
myExcelWorker.AskToUpdateLinks = False
myExcelWorker.AlertBeforeOverwriting = False
myExcelWorker.FeatureInstall = msoFeatureInstallNone
' Open the Workbook
Dim oWorkBook
Dim strWorkerWB
strWorkerWB = "C:\Users\Desktop\service calibration details\CC.xlsm"
'MsgBox "Opening filter"
on error resume next
Set oWorkBook = myExcelWorker.Workbooks.Open(strWorkerWB)
if err.number <> 0 Then
' Error occurred - just close it down.
MsgBox "open Error occurred"
End If
err.clear
on error goto 0
'MsgBox "Running macro"
Dim strMacroName
strMacroName = "CCA"
on error resume next
' Run the macro
myExcelWorker.Run strMacroName
if err.number <> 0 Then
' Error occurred - just close it down.
MsgBox "run macro Error occurred"
End If
err.clear
on error goto 0
' Clean up and shut down
Set oWorkBook = Nothing
myExcelWorker.Quit
Set myExcelWorker = Nothing
Set WshShell = Nothing
我試圖運行這個使用基於Windows腳本宿主。但我收到錯誤「運行宏錯誤發生」。
我在互聯網上搜索。但我找不到解決辦法。
是什麼導致了這個問題?
在我寫的vbscript中是否有錯誤?
我該如何成功執行此操作?
「_error occurred_」消息沒有診斷價值。加'&「0x」&十六進制(Err.Number)&「」&CStr(Err.Number)&「」&Err.Description「,那麼你可以得到更多的信息來找到一個罪魁禍首。提示:'Run'方法在新的Windows進程中啓動_program_運行... – JosefZ 2015-04-02 08:23:44
@JosefZ我應該在哪裏添加它? – adrian 2015-04-03 02:56:04
'MsgBox「bla-bla發生錯誤」&「0x」&Hex(Err.Number)&「」&CStr(Err.Number)&「」&Err.Description'。對於所有'bla-bla':_run宏_,_open_,... – JosefZ 2015-04-03 03:00:35