运行 vba 脚本时,出现宏不可用或所有宏可能被禁用的错误

发布于 2025-01-15 02:56:53 字数 1352 浏览 3 评论 0原文

我有几个脚本直到最近都运行良好。他们通过任务计划程序运行,当任务计划程序打开脚本时,它会得到以下内容: 错误显示message

第 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:
error display message

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:enter image description 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

痕至 2025-01-22 02:56:53

我无法理解它以前怎么能正常工作...基本上,您的 VBScript 代码应该完全限定要调用的过程。我的意思是,它还需要要调用的宏所在的工作簿名称(或全名)。请尝试:

 ExcelApp.run "'" & ExcelFilePath & "'" & "!" & MacroPath

"'"字符即使文件路径包含空格也能让代码运行良好...

理论上,使用工作簿全名会自动打开保留宏的工作簿,如果没有打开...我没有在 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:

 ExcelApp.run "'" & ExcelFilePath & "'" & "!" & MacroPath

"'" 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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文