【问题标题】:How to set recurring schedule for xlsm file using Windows Task Scheduler如何使用 Windows 任务计划程序为 xlsm 文件设置重复计划
【发布时间】:2014-05-11 08:37:31
【问题描述】:

我有一个xlsx 启用宏的文件。如何在任务管理器中进行设置,以便每天早上 9 点任务管理器打开工作簿、触发宏并关闭工作簿。

到目前为止,我正在使用

Application.OnTime . . .

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

【问题讨论】:

  • 您希望宏在工作簿打开时触发,然后自动关闭工作簿?
  • 没错。这就是我想要的
  • 这应该会有所帮助 - stackoverflow.com/questions/3957758/… 只需将代码放在您的工作簿中,而不是放在单独的模块中
  • @AndreiVasilev 您意识到这意味着用户根本不会与此工作簿交互,因为代码将运行然后自行关闭。这似乎是一个奇怪的请求,但 @Acantud 是正确的 将您的代码添加到 Workbook Open 事件将满足您的需求。
  • 不会有用户。理想情况下,我的宏应该在 vbscript 中。但由于我是 vba 的菜鸟,不知道如何写 vbscript 我唯一能想到的解决方案就是这个

标签: excel vbscript vba


【解决方案1】:

按照您的指示,最好使用

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

Running Excel on Windows Task Scheduler查看后一种方法的示例

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

【讨论】:

    【解决方案2】:

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

    简单

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

    更多细节...

    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. 最后,不要开启“无论用户是否登录都运行”

    应该可以的。

    告诉我!

    罗德·鲍文

    【讨论】:

      【解决方案3】:

      我推荐了 Kim 的一个博客来做这件事,它对我来说很好用。 See the blog

      宏的自动执行可以借助 VB 脚本文件来完成,该文件由 Windows 任务计划程序在指定时间调用。

      请记住将“YourWorkbook”替换为您要打开的工作簿的名称,并将“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
      

      您可以在命令提示符下测试这个 VB 脚本:

      >> cscript.exe RunExcel.VBS
      

      一旦您对 VB 脚本文件和工作簿进行了测试,使其能够执行您想要的操作,您就可以使用 Microsoft 任务计划程序(控制面板-> 管理工具-> 任务计划程序)执行 'cscript.exe RunExcel.vbs ' 自动为您服务。

      请注意宏的路径应该是正确的格式,并且在单引号内,例如:

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

      【讨论】:

        【解决方案4】:

        以下代码复制自 -> Here

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

        设置您的 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 打开,以便它将在后台执行,并且不会因 vbcript 启用的任何错误处理而挂起。

        【讨论】:

          【解决方案5】:

          我找到了一种更简单的方法,希望它对你有用。 (使用 Windows 10 和 Excel 2016)

          新建一个模块并输入以下代码: 子 auto_open() '要运行的宏(不必在此模块中,只需在此工作簿中 结束子

          通过任务计划程序设置任务并设置“程序运行为”Excel(在 C:\Program Files (x86)\Microsoft Office\root\Office16 找到我的)。然后将“添加参数(可选):设置为启用宏的工作簿的文件路径。记住 Excel 的路径和工作簿的路径都应该用双引号引起来。

          *参见 Rich 的示例,由 Community 编辑,获取 Windows 调度程序屏幕的图像。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2010-09-22
            • 1970-01-01
            • 1970-01-01
            • 2013-07-18
            • 2017-03-17
            • 2019-08-01
            • 2012-03-26
            相关资源
            最近更新 更多