Search code examples
batch-filevbscriptwindows-task-scheduler

How to run a .bat file in task scheduler


I have a .bat file that is one line long and calls up a vb script that specifically opens an excel file and runs some macros:

cscript priordays.vbs "C:\PATHANME\FILENAME.xlsm"

When I run it by double clicking, it runs fine and successfully.

When I run in task scheduler, it says it ran successfully (code 0x0), but the script does not execute. I've got it set to run with highest permissions, regardless of whether or not the user is logged in, wake machine, etc. Still nothing.

For those asking what's in the vb script, it's:

Dim arts, objExcel

Set args = WScript.ARguments
Set objExcel = CreateObject("Excel.Application")

objExcel.Workbooks.Open args(0)
objExcel.visible = True

objExcel.Run "Macro1"
objExcel.Run "Macro2"
objExcel.Run "Macro3"

objWorkbook.Close

objExcel.Quit

I found that code elsewhere on this site, along with the instructions on using the .bat file. If there's a way to run it all from the script, and try to launch the script from task scheduler, I'm fine to do that as well.


Solution

  • When running batch files and/or vbs from a scheduled task, the full file path and filename must be declared.

    If a file system object is used to get a filname, then passed (without the path) to a function that interacts with the file it will work from a double click, but not as a scheduled task.

    Can you try with the full path and in the .vbs file?

    Does your scheduled task launch excel, but not run the excel macro, or does it not even launch excel?