I have an Excel macro that I'm automating to run at 4am each day as it's a long running macro that can take up to 2 hours due to the amount of data that has to be parsed and can stop the user that's running the process from doing any other work. To do this, I have a task set up in the Task Scheduler that runs a vbs file to load Excel, run the macro, then save a copy ready for users to come in to at the start of the business day. The vbs script is successfully running, however when I open the file afterwards, it appears as though the macro hasn't completed as there isn't a separate file that's been saved and data tables appear blank. I think this is due to the vbs not allowing enough time to complete the macro before it closes Excel. Here's my script:
ExcelFilPath = "...\Documents\TestBook.xlsm"
MacroPath = "Module1.Macro1"
Set ExcelApp = CreateObject("Excel.Application")
ExcelApp.Displayalerts = False
Set wb = ExcelApp.Workbooks.Open(ExcelFilePath)
ExcelApp.Run MacroPath
wb.SaveAs = "...\Documents\TestBookComplete.xlsm"
ExcelApp.DisplayAlerts = True
wb.Close
ExcelApp.Quit
MsgBox "Task successfully run at " & TimeValue(Now), vbInformation