由于刷新过程中弹出的 excel 消息框,Wscript 无法运行 .vbs 脚本

问题描述

任务:我目前正在通过 Windows 任务计划程序处理自动化任务,因此用户可以获得每日/每周报告。

问题:我正在从 .bat 文件运行 .vbs 脚本。 .vbs 脚本中的宏会刷新报表中的所有数据透视表/切片器并保存更新后的报表,然后关闭 Excel 报表应用程序。

在测试期间,我注意到 .bat 文件在运行刷新时停止,因为 Excel 报告出现以下 msgBox 消息,然后需要 YES/NO 以便此后继续另一个 msg 框要求单击 YEs刷新后替换当前文件。 以下是 Excel 中出现的两个 msgBox

Blockquote

Blockquote

问题:我如何编写 .vbs 脚本以自动对 msg 框单击“是”而不是手动执行,如果需要任何手动工作,则没有自动化点。我需要一个干净流畅的自动化,我尝试以不同的方式编写 .vbs 脚本。

下面的 .vbs 脚本:

'Input Excel File's Full Path
'ExcelFilePath = ***Excel report file path
  
'Input Module/Macro name within the Excel File
  MacroPath = "RefreshReport"

'Create an instance of Excel
  Set ExcelApp = CreateObject("Excel.Application")

'Do you want this Excel instance to be visible?
  ExcelApp.Visible = True 'or "False"

'Prevent any App Launch Alerts (ie Update External Links)
  ExcelApp.displayAlerts = False 'or "True"

'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 'or "False"

'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

解决方法

您是否尝试在脚本中发送 {Enter} 以接受 MsgBox?

set shell = createobject("wscript.shell")
shell.sendkeys ("{Enter}")

https://ss64.com/vb/sendkeys.html