Detecting Excel App Close on Windows

Hey guys,

I’m using Xojo’s Office Automation in Windows, (Excel) and all is well until the spreadsheet I’m working with closes from some exterior influence, ie., user intervention, etc.

Is there a way my Xojo code can detect the Excel application closing?

Thanks, Ken.

@Eugene_Dakin is the resident expert this on this. If you don’t already have his book about using Excel with Xojo it is a must have.

I think the automation will throw an error if there is a process currently running updating the spreadsheet but not sure if it is just sitting idle between ops and it closes.

2 Likes

Hello @Ken_Boyle,

I do not know how to detect if the Excel Application is closing, and I know of a workaround to check if the app is opened or closed, and have attached an example program: ExcelClosed

Open the example program and build an executable. When running the program:

  1. press ‘Check if Excel is closed’ button, and it will show ‘Excel is not open’
  2. Press ‘Open Excel’ button, then press ‘Check if Excel if closed’ button. It will show ‘Excel is open’
  3. Close the Excel app and press ‘Check if Excel if closed’ button. It will show ‘Excel was opened, and is now closed’.

This workaround works on the ActiveWindow caption of the Excel file that is open.

There is a global ExcelApplication variable and code in the Open Excel button is:

Detection code in the ‘Check if Excel is closed’ button is shown below:

You may want to put the detection code on a timer to poll the result occasionally.

Thanks for the kind words @Joseph_E

Edit: This example was built with: Xojo 2021r2.1, on Windows 11, with the MSOfficeAutomation.rbx plugin from Xojo.

1 Like

Why don’t you simply list processes and check whether Excel is in the list?
The MBS plugin, or declares, can be used for that.

2 Likes

Thanks @Joseph_E, I recently purchased I_Wish_Excel_r4.0_Jun2021Rev1 but I haven’t been through it properly yet.

Thanks for responding. :+1:

Wow! Thanks for the ‘detailed’ explanation @Eugene_Dakin … greatly appreciated!!

As a side, I’d also like to mention how much value your books bring to the subject. I generally don’t read them cover to cover, but more to find a solution to a specific problem. I bought your lastest one (after previously buying the API 1 version) to figure out how to search in Excel. I was pulling my hair out when Xojo threw each time a match couldn’t be found. Then I read the section explaining that this was normal in the debug version and fine in the runtime. Thank you!!

2 Likes

Thanks for the suggestion @Arnaud_N, I’ll check this approach out.

Ken.

Ok, so this is what I did to get a workable solution:


If (Not GL_Tracker.IsOpen()) Then
// re-open Tracker SS (possibly closed externally?)
Open()
Return retVal
End If


// IsOpen method on Tracker class
Using Debug

Print(CurrentMethodName + “(” + “)”)

Var retVal As Boolean = True
Var activeCaption As String

If (Not Debug.DSSAssert(CurrentMethodName, “TR_Excel”, TR_Excel, Nil, AS_NOTNIL, DENDebug.AssertMessages.QuitWithErrorMessage)) Then Return False

Try
activeCaption = TR_Excel.ActiveWindow.Caption
Catch
retVal = False
End Try

Return retVal

Exception err As OLEException
MessageDialog.Show err.message

Thanks again for the help.
Cheers, Ken.

1 Like