【发布时间】:2020-07-21 04:57:37
【问题描述】:
我有一个在命令行上执行或双击时可以正确运行的 vb 脚本。该脚本打开一个 Excel 文件,运行一个宏,保存该文件,然后关闭该文件。但是,当我尝试设置 Windows 调度程序时,脚本不会保存 excel 文件。相反,它只是将其输出到我的 User/AppData/Local/Temp 文件夹中的文本文件中。为什么它不保存原始 excel 文件,我怎样才能让它做到这一点,而不是我的 Temp 文件夹中的文本文件?
编辑:我已经调查了原因,但找不到任何可行的方法或解释原因。
'Input Excel File's Full Path
ExcelFilePath = "C:\Users\UserID\Desktop\TestingVB4.xlsm"
'Input Module/Macro name within the Excel File
MacroPath = "Module1.Testing1"
'Create an instance of Excel
Set ExcelApp = CreateObject("Excel.Application")
'Do you want this Excel instance to be visible?
ExcelApp.Visible = False 'or "True"
'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
'Close Excel File
wb.Close
'Reset Display Alerts Before Closing
ExcelApp.DisplayAlerts = False
'End instance of Excel
ExcelApp.Quit
'Leaves an onscreen message!
MsgBox "Your Automated Task successfully ran at " & TimeValue(Now), vbInformation
【问题讨论】:
-
尝试注释掉
msgbox部分。
标签: excel vbscript windows-task-scheduler