简体   繁体   中英

when running vba script, getting error that macro is not available or all macros may be disabled

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM