【问题标题】:Windows Scheduler doesn't save excel file [duplicate]Windows调度程序不保存excel文件[重复]
【发布时间】: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

【问题讨论】:

标签: excel vbscript windows-task-scheduler


【解决方案1】:

您可以改用SaveAs,因为您有文件名。这应该强制它保存到正确的文件而不是临时文件:

wb.SaveAs ExcelFilePath

【讨论】:

  • 不确定这会有所不同,这可能是因为 AV 程序在创建临时文件时正在读取临时文件,因此 Excel 无法将其删除并将更改保存到原始文件。见Description of the Way That Excel Saves Files。
  • 所以我将调度程序中的设置从用户可以登录或关闭更改为刚刚登录,现在它可以正常工作了。虽然我需要它在注销时运行
猜你喜欢
  • 2021-10-03
  • 2013-08-17
  • 2011-05-02
  • 2011-05-22
  • 2016-03-07
  • 1970-01-01
  • 1970-01-01
  • 2015-03-30
  • 1970-01-01
相关资源
最近更新 更多