简体   繁体   中英

Using Windows Scheduler to run a .vbs file (via a batch file?)

I have an Excel file which contains queries from MS access which need to be refreshed at 6am every Monday. Currently we use Business Objects to schedule this, but we are trying to move away from this, hence using Access/Excel.

Currently I have a .vbs file:

 Set fs = CreateObject("Scripting.FileSystemObject")
 Set rootFolder = fs.GetFolder(fs.GetParentFolderName(wscript.ScriptFullName))
 Set oExcel = CreateObject("Excel.Application") 

 oExcel.Visible = True
 oExcel.DisplayAlerts = False
 oExcel.AskToUpdateLinks = False
 oExcel.AlertBeforeOverwriting = False

 For Each file in rootFolder.Files
   If inStr(file.type, "Excel") > 0 Then
     Set oWorkbook = oExcel.Workbooks.Open("Q:\Secure\testing.xlsm")
     oWorkbook.RefreshAll
     WScript.Sleep 1000 * 60
     oWorkbook.Save
     oWorkbook.Close
     Set oWorkbook = Nothing
   End If
 Next

 oExcel.Quit
 Set oExcel = Nothing

This opens the excel file in question, refreshes the queries in it (allowing one minute for this), then saves and closes the excel file. I can manually open this .vbs file and it works as expected, however I don't want to have to do this manually at 6am every Monday. I then tried to combine this with Windows Scheduler and it wouldn't play ball (returned a launch/run failure). After some searching I saw some suggestions of using a .bat file to open the .vbs (via Windows Scheduler). My attempt at the .bat file is below:

@echo off
Start "" "Q:\Secure\Automation.vbs"

Again, when I run this manually it does as expected. When I tried running this through Windows Scheduler it says it has run successfully, with no error messages shown either in the scheduler or in a pop-up. The issue here is that the excel file hasn't actually updated or saved. Can anyone point me in the right direction here? I thought that both my .vbs and .bat files were okay as they work when running manually, that may not be the case it seems...

Don't use a batch file to run it. Simply use...

cscript.exe "Q:\Secure\Automation.vbs"

... as your command in Task Scheduler.

Is Q: a network drive? You need to set your scheduled task to run with credentials that can access that network resource and also map the drive in your batch file, something like this:

@ECHO OFF
NET USE Q: \\YourServerName\ShareName
START "" "Q:\Secure\Automation.vbs"

I usually just write a batch file to call the file to run the macro. I use this for many projects without fail. Be sure to have macro inside a module. ETA: Then schedule the batch file course

Dim xlApp
Dim xlWkb
Set xlApp = CreateObject("excel.application")
Set xlWkb = xlApp.Workbooks.Open("PATH TO YOUR FILE")
xlApp.Visible = True
xlWkb.RunAutoMacros 1 'enables macros to be run on open
xlApp.Run ("YOUR PROCEDURE")
xlApp.Workbooks("YOUR WORKBOOK NAME").Save 'Save the workbook
xlApp.Quit 'quits excel

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM