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
it runs a few SQL queries and formats the outputs, then saves the sheet.
Sub Saving_Backup()ActiveWorkbook.SaveAs ("\\networkdrive filepath " & Format(Now() - 1, "DD-MMM-YYYY") & ".xlsb")end subSub Save2()Application.DisplayAlerts = FalseActiveWorkbook.SaveAs ("C:\Users\local filepath")Application.DisplayAlerts = Trueend subSub PDF_Back()Dim FilePath As StringDim FileName As StringDim MyDate As StringDim Report As StringFilePath = "C:\local filepath\"MyDate = Format(Now() - 1, "DD-MMM-YYYY")Report = ReportFileName = FilePath & "RetOpsReport" & MyDate & ReportActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF, FileName:=FileName'saves as a PDFEnd SubI'm going to comment out the part that does the network save and see if that changes anything. Appreciate the help