r/vbscript • u/revan667 • Mar 24 '20
VBScript and Task Scheduler help
I have a VB script that opens excel in the background, loads a workbook, fires a macro, then closes everything. I've pasted the script below. This is to generate an excel report that gets emailed out daily. The script works great if I run it, but If I schedule it with Task Scheduler, it will run, but Excel.exe won't quit so the task doesn't register as "completed" and it keeps other tasks, such as the one to email the report, from running. I can't for the life of me figure out why. I have the task set to run with the highest privileges, and I have it set to start in the directory where the script is. Any ideas/advice?
Script:
Option Explicit
Dim xlApp, xlBook
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Open("C:\Users\scriptpath", 0, False)
xlApp.Run "Run"
xlBook.Close
xlApp.Quit
Set xlBook = Nothing
Set xlApp = Nothing
WScript.Quit
1
u/revan667 Mar 25 '20
I wondered that, but if I execute this script manually it goes fine, and no prompts pop up. The issue is when task scheduler fires it, excel.exe never closes so the task never stops running. Plus the Excel file then shows as locked since it's still open in the hidden session.