How to close all open Excel workbooks with VBS - excel

I automate a process using a .vbs file and sometimes I forget to close the excel file at night, so when the process runs the file opens as a read-only causing issues. I'd like to just close or end all instances of open workbooks.
I thought a ".Quit" would do the trick but doesnt seems to close workbooks that are currently open. I test .quit in a macro and it does indeed close all open workbooks and the excel app in general .
Any ideas?
Set ExcelApp = CreateObject("Excel.Application")
ExcelApp.Applicaiton.Quit
'reopen actual file as non read only
Set ExcelApp = CreateObject("Excel.Application")
ExcelApp.Visible = True
ExcelApp.DisplayAlerts = False
ExcelFilePath = "C:\Users\user\Documents\test.xlsm"
Set wb = ExcelApp.Workbooks.Open(ExcelFilePath)

Instead of
Set ExcelApp = CreateObject("Excel.Application")
try
Set ExcelApp = GetObject(, "Excel.Application")
To bind to existing excel instance.

Related

VBA does not suppress save option prompt for online files

I'm using Access VBA to auto update a shared Excel file - create XL object, open the shared file by sharepoint path, make necessary updates and close the file. In the codes, the displayalerts is set to False
Most of the time it works ok, but occasionally upon closing the file, it gives this prompt, even if the displayalerts is set to false.
When this happens, how do I auto select "Discard my change" by vba code?
Here's the code:
Dim XL As Excel.Application
Dim wkb As Excel.Workbook
Set XL = CreateObject("Excel.Application")
XL.Visible = False
XL.DisplayAlerts = False
Set wkb = XL.Workbooks.Open(SharePointFilePath)
'do stuff
wkb.Save
wkb.Close
XL.Quit
Set XL = Nothing

VBA to get open Excel Binary Workbook

I have an MS Access form that opens an Excel binary file (.xlsb) from another website. I am trying to check all the user's open Excel files so that I work on the correct workbook.
By running the loop below on the opened, but not saved Excel file, the GetObject does not find the just opened workbook. This code does find other Excel files that I might already have open.
But, if I save the Excel file that I opened from the network, close and reopen it before I try to find the open Excel files with the code below, the code finds that file too.
Is there a better way for me to capture the just-opened file name? On the new Excel file, I need to filter data from particular Excel tabs and add that data to Access tables.
Dim xlApp As Excel.Application
Dim strWBList As String
strWBList = ""
On Error Resume Next
Set xlApp = GetObject(, "Excel.Application")
If Err.Number = 0 Then
Dim xlWB As Excel.Workbook
For Each xlWB in xlApp.Workbooks
If Len(strWBList) > 0 Then
strWBList = strWBList & ","
End If
strWBList = strWBList & xlWB.Name
Next xlWB
Set xlApp = Nothing
Set xlWB = Nothing
End If
MsgBox strWBList

plugin error missing when opening excel file with VB script

I try to write a piece of code with VB script to open an excel file, take a print screen and send it by email.
This is on my professional laptop and I have several plugin installed on excel.
The excel file in question has a macro inside which run on each opening but does nothing fancy (mostly formatting data).
When I open the workbook normally, everything works fine.
When I launch the VB script, I have a error with a missing xla file (refer to file attached)
The code is quite simple:
Dim Xl 'as Excel.Application\par
Dim wk 'as Excel.workbook\par
set Xl = createobject("Excel.application")
Xl.Visible = True
Xl.enableevents = True
set Wk = Xl.workbooks.open("XXX")
Try to open workbook via shell:
Option Explicit
Dim oExcelApp, oWorkbook
CreateObject("Shell.Application").ShellExecute "C:\Test\Test.xlsm"
Do
On Error Resume Next
Set oExcelApp = GetObject(, "Excel.Application")
WScript.Sleep 5
Loop While Err
On Error Goto 0
Set oWorkbook = oExcelApp.Workbooks("Test.xlsm")
oWorkbook.Sheets(1).Cells(1, 1).Value = Now()

How to exit gracefully closing open excel file on error?

I use dos .bat file to run a .vbs file continuously ... If any error occurs the batch file runs the vbscript again and it goes on. The script connects via internet to a site to execute some api calls.
Now when there is connection problem or any other error then the control comes out of the script keeping the excel file open. This way many excel files gets open on each error ... The code is as follows ... please advice me how to close the excel file on error and then come out of the script gracefully.
'{{{{Some coding}}}}
dim objExcel, objWorkbook, objRange
Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open("C:\temp.xlsx")
objExcel.Visible = True
'{{{{Some coding}}}}
objExcel.Cells(xlrow, 3).Value="Test"
objExcel.Cells(xlrow, 3).Select
objExcel.Activecell.Show
'{{{{Some coding}}}}
objExcel.Workbooks(1).save
objExcel.Workbooks(1).close
objExcel.Quit
Set objExcel = Nothing
Set objWorkbook = Nothing
WScript.Quit
Thanks in advance
One possible approach is to wrap the Excel handling in a custom class:
Class Excel
Private xl
Private Sub Class_Initialize
Set xl = CreateObject("Excel.Application")
End Sub
Private Sub Class_Terminate
For Each wb In xl.Workbooks
wb.Saved = True 'discard unsaved changes
wb.Close 'close workbook
Next
xl.Quit 'quit Excel
End Sub
Public Function OpenWorkbook(filename)
Set OpenWorkbook = xl.Workbooks.Open(filename)
End Function
Public Function NewWorkbook
Set NewWorkbook = xl.Workbooks.Add
End Function
Public Property Get Workbooks
Set Workbooks = xl.Workbooks
End Property
End Class
The procedure Class_Terminate is automatically called whenever a class instance is destroyed. That way you can automatically close the open workbooks and quit Excel.
The class can be used like this:
Set xl = New Excel
Set wb = xl.OpenWorkbook("C:\path\to\your.xlsx")
...
wb.Close

VBScript code to keep Excel file open

I have a VBScript code to open an excel file, run a macro and close it. Fine.
Now, the only thing I want to change is to leave the file open.
If I remove the 2 lines of code xlApp.activewindow.close and xlApp.Quit, then the workbook and the application are closed anyway, but they remain open in the background (Excel process still active in Task Manager). Hence, it is impossible to re-run the macro later on the same file by calling the script again (which is exactly what I want to do).
Why?
Here is the code:
Option Explicit
On Error Resume Next
MyTest
Sub MyTest()
Dim xlApp
Dim xlBook
Dim fpath
Dim fname
' Excel application running? if not, open Excel
On Error Resume Next
Set xlApp = GetObject(, "Excel.Application")
If xlApp <> "Microsoft Excel" Then
Set xlApp = CreateObject("Excel.Application")
End If
Err.Clear
' correct Excel file open? if not, open it
fpath = "D:\Desktop\"
fname = "MyTest.xls"
xlApp.Workbooks(fname).Activate
If Err = 0 Then
' no error, so it has been possible to activate the workbook
Set xlBook = xlApp.Workbooks(fname)
Else
' unable to activate, so workbook was not open -> open it now
Set xlBook = xlApp.Workbooks.Open(fpath & fname, 0, True)
End If
Err.Clear
' now run the desired macro in the excel file
xlApp.Run "HelloWorld"
' WANT TO CHANGE THIS
xlBook.saved = True
xlApp.activewindow.close
' AND THIS
xlApp.Quit
Set xlBook = Nothing
Set xlApp = Nothing
End Sub
You just need to make your new instance of Excel visible. Do this right after creating it:
xlApp.Visible = True
This line of code will close your current activated workbook (by now, it is D:\Destop\MyTest.xls);
xlApp.activewindow.close
This line will quit Excel application;
xlApp.Quit

Resources