我有一个启用xlsx宏的文件。如何在任务管理器中设置它,以便每天上午9点任务管理器打开工作簿、启动宏和关闭工作簿。
到目前为止我正在使用
Application.OnTime . . .
但是我意识到让xlsm文件保持打开是不方便的。
发布于 2014-03-31 22:48:10
如您所示,最好使用vbs
vbs,它是一个具有.vbs扩展名的文本文件(参见下面的示例代码)vbsvbs在预定的时间打开workbook,然后选择:ThisWorkbook模块中的ThisWorkbook事件运行代码Application.Run中使用vbs来运行宏
请参阅在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发布于 2014-12-09 01:52:33
我提到了Kim的博客,因为他做了这件事,而且对我来说也很好。见博客
宏的自动执行可以通过一个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您可以从命令提示符中测试此VB脚本:
>> cscript.exe RunExcel.VBS一旦对VB脚本文件和工作簿进行了测试,使其能够满足您的需要,您就可以使用Microsoft (控制面板->管理工具->任务调度程序)自动执行“cscript.exe RunExcel.vbs”。
请注意宏的路径应该是正确的格式和单引号,如下所示:
strMacroName = "'" & strPath & "\YourWorkBook.xlsm'" &
"!ModuleName.MacroName"发布于 2016-07-28 05:31:03
三个重要步骤--如何调度excel.xls(m)文件
简单
更详细的.
`
' 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
'`
设置程序/脚本:= C:\Windows\System32\cscript.exe
设置添加参数(可选):= C:\MyVbsFile.vbs
那应该管用。
让我知道!
棒状鲍恩
https://stackoverflow.com/questions/22771185
复制相似问题