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

And*_*lev 9 vbscript excel vba excel-vba

我有一个xlsx启用宏的文件.如何在任务管理器中设置它,以便每天上午9点任务管理器打开工作簿,激活宏并关闭工作簿.

到目前为止我正在使用

Application.OnTime . . .

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

bre*_*tdj 14

最好像你指示的那样使用

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

请参阅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
Run Code Online (Sandbox Code Playgroud)


小智 6

三个重要步骤 - 如何安排 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 
'
Run Code Online (Sandbox Code Playgroud)

`

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

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

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

  1. 最后,不要打开“无论用户是否登录都运行”

那应该工作。

让我知道!

罗德鲍文


rch*_*cko 5

我推荐了Kim撰写的博客,它对我来说很好用。看博客

宏的自动执行可以借助Windows Task Scheduler在指定时间调用的VB脚本文件来完成。

请记住,用要打开的工作簿的名称替换“ 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
Run Code Online (Sandbox Code Playgroud)

您可以从命令提示符下测试此VB脚本:

>> cscript.exe RunExcel.VBS
Run Code Online (Sandbox Code Playgroud)

一旦对VB脚本文件和工作簿进行了测试,使其可以完成所需的工作,则可以使用Microsoft任务计划程序(“控制面板”->“管理工具”->“任务计划程序”)自动执行“ cscript.exe RunExcel.vbs”您。

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

strMacroName = "'" & strPath & "\YourWorkBook.xlsm'" & 
"!ModuleName.MacroName"
Run Code Online (Sandbox Code Playgroud)