运行 vba 脚本时,出现宏不可用或所有宏可能被禁用的错误
我有几个脚本直到最近都运行良好。他们通过任务计划程序运行,当任务计划程序打开脚本时,它会得到以下内容:
第 20 行是这样的:
'Execute Macro Code
ExcelApp.Run MacroPath
这是完整的脚本:
'Input Excel File's Full Path
ExcelFilePath = "C:\vba-files\task_sched\task_modules.xlsm"
'Input Module/Macro name within the Excel File
MacroPath = "Module1.get_data"
'Create an instance of Excel
Set ExcelApp = CreateObject("Excel.Application")
'Do you want this Excel instance to be visible?
ExcelApp.Visible = false
'Prevent any App Launch Alerts (ie Update External Links)
ExcelApp.DisplayAlerts = False
'Open Excel File
Set wb = ExcelApp.Workbooks.Open(ExcelFilePath)
'Execute Macro Code
ExcelApp.Run MacroPath
'Save Excel File (if applicable)
wb.Save
'Reset Display Alerts Before Closing
ExcelApp.DisplayAlerts = True
'Close Excel File
wb.Close
'End instance of Excel
ExcelApp.Quit
'Leaves an onscreen message!
MsgBox "Your Automated Task successfully ran at " & TimeValue(Now), vbInformation
当打开工作簿并手动运行宏时,它可以正常工作。 它只是在运行脚本时出错
I have several scripts that were working fine until recently. They were running through a task scheduler and when the task scheduler opens the script it gets the following:
line 20 is this:
'Execute Macro Code
ExcelApp.Run MacroPath
here is the full script:
'Input Excel File's Full Path
ExcelFilePath = "C:\vba-files\task_sched\task_modules.xlsm"
'Input Module/Macro name within the Excel File
MacroPath = "Module1.get_data"
'Create an instance of Excel
Set ExcelApp = CreateObject("Excel.Application")
'Do you want this Excel instance to be visible?
ExcelApp.Visible = false
'Prevent any App Launch Alerts (ie Update External Links)
ExcelApp.DisplayAlerts = False
'Open Excel File
Set wb = ExcelApp.Workbooks.Open(ExcelFilePath)
'Execute Macro Code
ExcelApp.Run MacroPath
'Save Excel File (if applicable)
wb.Save
'Reset Display Alerts Before Closing
ExcelApp.DisplayAlerts = True
'Close Excel File
wb.Close
'End instance of Excel
ExcelApp.Quit
'Leaves an onscreen message!
MsgBox "Your Automated Task successfully ran at " & TimeValue(Now), vbInformation
macro settings are enabled as seen here:
when the workbook is opened and the macro is ran manually it works fine.
it is just erroring out when ran through the script
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我无法理解它以前怎么能正常工作...基本上,您的 VBScript 代码应该完全限定要调用的过程。我的意思是,它还需要要调用的宏所在的工作簿名称(或全名)。请尝试:
"'"
字符即使文件路径包含空格也能让代码运行良好...理论上,使用工作簿全名会自动打开保留宏的工作簿,如果没有打开...我没有在 VBScript 的自动化上测试它。从 Excel 进行调用时以这种方式工作(已测试)
I cannot understand how could it work fine before... Basically, your VBScript code should fully qualify the procedure to be called. I mean, it also needs the workbook name (or full name) where the macro to be called exists. Please, try:
"'"
characters make the code running well even if the file path contains spaces...In theory, using the workbook full name will automatically open the workbook keeping the macro, if not open... I did not test it on automation from VBScript. It works in this way when make the call from Excel (tested)