首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用Windows任务调度程序设置xlsm文件的重复调度

如何使用Windows任务调度程序设置xlsm文件的重复调度
EN

Stack Overflow用户
提问于 2014-03-31 20:07:19
回答 5查看 124.5K关注 0票数 11

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

到目前为止我正在使用

Application.OnTime . . .

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

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2014-03-31 22:48:10

如您所示,最好使用vbs

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

请参阅在Windows任务调度程序上运行Excel后面的方法示例

样本vbs

代码语言:javascript
复制
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
票数 16
EN

Stack Overflow用户

发布于 2014-12-09 01:52:33

我提到了Kim的博客,因为他做了这件事,而且对我来说也很好。见博客

宏的自动执行可以通过一个VB脚本文件来完成,该脚本文件在指定的时间由调用。

请记住将“YourWorkbook”替换为要打开的工作簿的名称,并将“YourMacro”替换为要运行的宏的名称。

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

代码语言:javascript
复制
    ' 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脚本:

代码语言:javascript
复制
>> cscript.exe RunExcel.VBS

一旦对VB脚本文件和工作簿进行了测试,使其能够满足您的需要,您就可以使用Microsoft (控制面板->管理工具->任务调度程序)自动执行“cscript.exe RunExcel.vbs”。

请注意宏的路径应该是正确的格式和单引号,如下所示:

代码语言:javascript
复制
strMacroName = "'" & strPath & "\YourWorkBook.xlsm'" & 
"!ModuleName.MacroName"
票数 6
EN

Stack Overflow用户

发布于 2016-07-28 05:31:03

三个重要步骤--如何调度excel.xls(m)文件

简单

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

更详细的.

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

`

代码语言:javascript
复制
'   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. 最后,不要打开“运行用户是否登录”

那应该管用。

让我知道!

棒状鲍恩

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22771185

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档