I have tried for two days to find a way to use a VBscript to close excel.
I have a vbscript set up in task scheduler that calls a restAPI, then opens excel, updates data model in workbook called RepublicReport. (This works great)
The data model update triggers file save using THISWORBOOK event. (this works great)
Then the THISWORKBOOK event SAVE triggers a macro that creates new copy of the excel worksheet, and emails the worksheet to client. (This works great)
Then it closes excel file RepublicREport (this works great)
Then IS SUPPOSE to close excel all together (Not working!!!!).
For some reason, after it all runs it opens a new instance of excel back up as BOOK1.xls. I created a vbscript to come behind the task 15 minutes later and close excel again, but it isn't closing the file.
Here is my code for THISWORKBOOK save event. (yes I have two separate pieces of code that are trying to close the workbook). It closes the RepublicReport workbook but opens a new version of excel as Book1.xls
Private Sub Workbook_AfterSave(ByVal Success As Boolean)
Dim objExcel
Dim objWorkbook
Call Macro2
Application.DisplayAlerts = False
ThisWorkbook.Close
ThisWorkbook.Saved = True
On Error Resume Next
Set objExcel = GetObject("C:\Republic_Standard_Report\RepublicReport.xlsm").Application
Set objWorkbook = objExcel.Workbooks("RepublicReport.xlsm")
If (Not objWorkbook Is Nothing) Then
objExcel.Workbooks("RepublicReport.xlsm").Saved = True
objExcel.Workbooks("RepublicReport.xlsm").Close
End If
On Error GoTo 0
objExcel.Quit
End Sub
Here is my vbScript to close excel that is in a separate instance of TaskScheduler.
Dim objExcel
Dim objWorkbook
On error resume next
Set objExcel = CreateObject("Excel.Application") 'Reference to ANY excel file on local pc
set objWorkbook = objExcel.Workbooks("Book1.xlsx") 'Name of file you want to close
if (not objWorkbook is nothing) then
objExcel.Workbooks("Book1.xlsx").Saved=True
objExcel.Workbooks("Book1.xlsx").Close
end if
on error goto 0
objExcel.quit
I am still learning, but I have tried several solutions I found on the internet. Originally when I nested them in the same task, they hopped over each other. So I split them thinking it would help, any suggestions are greatly welcomed. THANK YOU IN ADVANCE!!!