I have a very basic VBScript that I want to be able to call from Task Scheduler to open an Excel document, refresh all and then save and close it. I've been playing various bits of code I've found online (as I'm very new to this) and I can get it to open and refresh all, but the VBScript closes before it will save and/or close the workbook.
I have tried adding 'WScript.Sleep' (with various amounts of time) as well as DoEvents to allow Excel to finish, but the script still closes before saving and closing workbook.
Set objExcel = CreateObject("Excel.Application")
Set objWorkbook =
objExcel.Workbooks.Open("\\llfs01\ReportHub(Shoals)\Sales\Daily
Reports\F&I Daily Reports\CIT Report.xlsm")
objExcel.Application.Visible = True
objExcel.DisplayAlerts = False
objExcel.AlertBeforeOverwriting = False
objWorkbook.WorkSheets(1).Activate
objWorkbook.RefreshAll 'Refresh everything
objWorkbook.DoEvents 'Let Excel finish
WScript.Sleep 20000
objExcel.ActiveWorkbook.Save
DoEvents
objExcel.ActiveWorkbook.Close
I need the code to open the workbook (while my PC is locked and unattended), refresh all data within the workbook, save it and close the workbook.
DoEventsandsleepas they are unnecessary.. - catcatDoEventsis dangerous and should never be used to randomly solve a problem. See my answer here stackoverflow.com/questions/54176056/… - catcat