如何使用Windows任务计划程序为xlsm文件设置重复计划

我有一个xlsxmacros启用文件。 我如何在任务pipe理器中设置它,以便每天早上9点任务pipe理器打开工作簿,激发macros并closures工作簿。

到目前为止,我正在使用

Application.OnTime . . .

但是我意识到保持打开xlsm文件是不方便的

如您所示,最好使用vbs

  1. 创build一个简单的vbs ,这是一个扩展名为.vbs的文本文件(请参阅下面的示例代码)
  2. 使用任务计划程序来运行vbs
  3. 使用vbs在计划的时间打开workbook ,然后:
    • 在打开文件时,使用ThisWorkbook模块中的Private Sub Workbook_Open()事件来运行代码
    • 更强大的(因为打开时可能会禁用macros),请使用vbs Application.Run来运行macros

请参阅在Windows任务计划程序上运行Excel的稍后方法的示例

样本vbs

 Dim ObjExcel, ObjWB Set ObjExcel = CreateObject("excel.application") 'vbs opens a file specified by the path below Set ObjWB = ObjExcel.Workbooks.Open("C:\temp\rod.xlsm") 'either use the Workbook Open event (if macros are enabled), or Application.Run ObjWB.Close False ObjExcel.Quit Set ObjExcel = Nothing 

我提到了Kim的博客,因为这样做对我来说工作得很好。 看博客

macros的自动化执行可以在Windows任务计划程序在指定时间调用的VB脚本文件的帮助下完成。

请记住用您要打开的工作簿的名称来replace“YourWorkbook”,并用要运行的macros的名称replace“YourMacro”。

请参阅VB脚本文件(仅将其命名为RunExcel.VBS):

  ' Create a WshShell to get the current directory Dim WshShell Set WshShell = CreateObject("WScript.Shell") ' Create an Excel instance Dim myExcelWorker Set myExcelWorker = CreateObject("Excel.Application") ' Disable Excel UI elements myExcelWorker.DisplayAlerts = False myExcelWorker.AskToUpdateLinks = False myExcelWorker.AlertBeforeOverwriting = False myExcelWorker.FeatureInstall = msoFeatureInstallNone ' Tell Excel what the current working directory is ' (otherwise it can't find the files) Dim strSaveDefaultPath Dim strPath strSaveDefaultPath = myExcelWorker.DefaultFilePath strPath = WshShell.CurrentDirectory myExcelWorker.DefaultFilePath = strPath ' Open the Workbook specified on the command-line Dim oWorkBook Dim strWorkerWB strWorkerWB = strPath & "\YourWorkbook.xls" Set oWorkBook = myExcelWorker.Workbooks.Open(strWorkerWB) ' Build the macro name with the full path to the workbook Dim strMacroName strMacroName = "'" & strPath & "\YourWorkbook" & "!Sheet1.YourMacro" on error resume next ' Run the calculation macro myExcelWorker.Run strMacroName if err.number <> 0 Then ' Error occurred - just close it down. End If err.clear on error goto 0 oWorkBook.Save myExcelWorker.DefaultFilePath = strSaveDefaultPath ' Clean up and shut down Set oWorkBook = Nothing ' Don't Quit() Excel if there are other Excel instances ' running, Quit() will shut those down also if myExcelWorker.Workbooks.Count = 0 Then myExcelWorker.Quit End If Set myExcelWorker = Nothing Set WshShell = Nothing 

您可以从命令提示符下testing这个VB脚本:

 >> cscript.exe RunExcel.VBS 

一旦你有VB脚本文件和工作簿testing,以便它做你想要的,然后可以使用Microsoft任务计划程序(控制面板 – >pipe理工具 – >任务计划程序)自动执行“cscript.exe RunExcel.vbs”您。

请注意macros的path应该是正确的格式,并在单引号内,如:

 strMacroName = "'" & strPath & "\YourWorkBook.xlsm'" & "!ModuleName.MacroName" 

下面的代码从 – > 这里复制

首先,您必须将工作簿保存为支持macros的工作簿。 所以它需要是xlsm而不是xlsx 。 否则,由于未启用macros,Excel将禁用该macros。

设置你的VBScript(C:\ excel \ tester.vbs)。 示例子“test()”必须位于excel文档的模块中。

 dim eApp set eApp = GetObject("C:\excel\tester.xlsm") eApp.Application.Run "tester.xlsm!test" set eApp = nothing 

然后设置您的日程安排,给它一个名字,和一个用户名/密码离线访问。

那么你必须设置你的动作和触发器。

设定你的时间表(触发)

设置你的触发器

行动,设置您的vbscript与Cscript.exe打开,以便它将在后台执行,而不是挂起任何error handling该vbcript已启用。

操作属性

三个重要步骤 – 如何任务安排一个excel.xls(m)文件

简单说

  1. 确保.vbs文件是正确的
  2. 在任务计划程序中正确设置操作选项卡
  3. 不要打开“运行用户是否login”

在更多细节…

  1. 这里是一个例子.vbs文件

`

 ' a .vbs file is just a text file containing visual basic code that has the extension renamed from .txt to .vbs 'Write Excel.xls Sheet's full path here strPath = "C:\RodsData.xlsm" 'Write the macro name - could try including module name strMacro = "Update" ' "Sheet1.Macro2" 'Create an Excel instance and set visibility of the instance Set objApp = CreateObject("Excel.Application") objApp.Visible = True ' or False 'Open workbook; Run Macro; Save Workbook with changes; Close; Quit Excel Set wbToRun = objApp.Workbooks.Open(strPath) objApp.Run strMacro ' wbToRun.Name & "!" & strMacro wbToRun.Save wbToRun.Close objApp.Quit 'Leaves an onscreen message! MsgBox strPath & " " & strMacro & " macro and .vbs successfully completed!", vbInformation ' 

`

  1. 在“操作”选项卡(“任务计划程序”)中

设置程序/脚本:= C:\ Windows \ System32 \ cscript.exe

设置添加参数(可选):= C:\ MyVbsFile.vbs

  1. 最后,不要打开“运行用户是否login”

这应该工作。

让我知道!

Rod Bowen