2014-03-31 171 views
8

我有一個xlsx宏啓用文件。我如何在任務管理器中設置它,以便每天早上9點任務管理器打開工作簿,啓動宏並關閉工作簿。如何使用Windows任務計劃程序爲xlsm文件設置週期性日程安排

到目前爲止我使用

Application.OnTime . . .

但我意識到保持XLSM文件打開不方便

+0

你想在工作簿打開時觸發一個宏,然後自動關閉工作簿? – engineersmnky

+0

沒錯。這是我想要的 –

+1

這應該有所幫助 - http://stackoverflow.com/questions/3957758/vba-events-load-workbook-before-running-code-using-workbook-open只需將代碼放入工作簿,而不是在一個獨立的模塊 – Acantud

回答

8

更好地使用爲你指出

  1. 創建一個簡單的vbs,這是一個.vbs擴展名的文本文件(請參閱示例代碼b elow)
  2. 使用任務計劃程序運行vbs
  3. 使用vbs打開workbook在預定的時間,然後要麼:
    • 使用Private Sub Workbook_Open()事件ThisWorkbook模塊中運行代碼時,該文件是打開
    • 更有力(如宏可以開上被禁用),使用Application.Runvbs運行宏

看到後一種方法的這個例子中,在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 
3

代碼從下面複製 - >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 

然後設置你的日程安排,給它一個名稱,並進行離線訪問用戶名/密碼。

然後你必須設置你的動作和觸發器。

設置日程(觸發)

Set your trigger

行動,將您的VBScript與Cscript.exe來打開,這樣它會在後臺執行,並沒有得到任何錯誤處理vbcript掛了已啓用。

Action Properties

4

我提到一個博客由金做這個和它的工作對我來說很好。See the blog

可以在指定時間由Windows任務計劃程序調用的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 Task Scheduler(控制面板 - >管理工具 - >任務計劃程序)爲您自動執行「cscript.exe RunExcel.vbs」。

請注意宏的路徑應該是正確的格式和單引號裏面一樣:

strMacroName = "'" & strPath & "\YourWorkBook.xlsm'" & 
"!ModuleName.MacroName" 
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 
' 

`

  • 在操作選項卡(任務調度程序)
  • 設置程序/腳本:= C:\ Windows \ System32下\的Cscript.exe

    集添加參數(可選):= C:\ MyVbsFile.vbs

  • 最後,請勿打開「運行用戶是否已登錄」
  • 這應該有效。

    讓我知道!

    Rod Bowen

    0

    我發現一個更簡單的方法,我希望它適合你。 (使用的是Windows 10和Excel 2016)

    創建一個新的模塊並輸入以下代碼: 子auto_open() 「宏要運行的(不必是這個模塊中,只是在此工作簿 結束Sub

    通過任務計劃程序設置任務並設置「要運行的程序爲Excel」(可在C:\ Program Files(x86)\ Microsoft Office \ root \ Office16中找到),然後設置「Add參數(可選):作爲啓用宏的工作簿的文件路徑。請記住,Excel的路徑和工作簿的路徑應該用雙引號引起來。富由社區編輯的Windows調度程序屏幕圖像。

    相關問題