我有一个包含宏的excel文件。宏是在开发人员选项卡的ThisWorkbook中编写的。我想通过在Windows任务调度程序中安排宏来自动运行宏。
我做了一些研究,发现我必须为此编写一个vbscript并运行vb脚本以运行宏。
这是vb脚本。 vbscript应该:
这应该在每天的预定时间使用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
我尝试使用基于Microsoft Windows的脚本宿主来运行它。但是我得到了错误&#34;发生了宏错误&#34;。
我在互联网上搜索过。但我无法找到解决方案。是什么导致了这个问题?
我写过的vbscript是否有错误?
如何成功执行此操作?
答案 0 :(得分:2)
作为解决此问题的替代方法,您可以使用简单的VB脚本文件来打开电子表格。像这样:
Set xl = CreateObject("Excel.application")
xl.Application.Workbooks.Open "C:\Users\Desktop\service calibration details\CC.xlsm"
xl.Application.Visible = True
Set xl = Nothing
然后将excel宏放入Workbook_Open子中,以便在打开工作簿时执行。在宏的末尾添加以下行:
ActiveWorkbook.Close False 'false prevents it from saving any changes
Application.Quit
或者如果你想保存
ActiveWorkbook.Save
Application.Quit
这应该有希望成功!祝你好运。