(Solved) – Office365 Excel – How to close window if Workbook.close doesn’t work?

(solved)-–-office365-excel-–-how-to-close-window-if-workbook.close-doesn’t-work?

I somehow have this bug where another application has created an Excel workbook and this workbook is not closable from Excel. The application which created the workbook was a SAP custom application. We don’t have access to this app but we do have access to VBA. The crazy thing is the vba:

myWorkbook.close

Does not close the workbook and instead reports Application Defined or Object Defined error.

In previous versions of Excel calling myWorkbook.close would cause VBA to crash but leave Excel unaffected, so I already know something is amiss, but not sure what… Anyway here are some things I tried to work around the issue:

  • On Error Resume Next – just skips closing.
  • Closing the SAP App which created the Excel window/workbook.
  • Send keys Alt F4 – doesn’t seem to register the sent keys
  • Application.DisplayAlerts = false
  • Application.IgnoreRemoteRequests = false
  • Application.EnableEvents = false
  • CloseWindow(stdAcc.FromExcel().hwnd) call CloseWindow Win32 API function
  • Use stdAcc.FromExcel().SendMessage(...) to send various messages to the application window including WM_CLOSE, WM_QUIT, WM_DESTROY, … These raised another error message Cannot quit Microsoft Excel.
  • Use stdAcc to click on the red cross – didn’t seem possible as apparrently the red cross is virtual and does not implement IAccessible interface…
  • User manually clicks on red cross. This works, but it’s sad that we cannot automate this…

Leave a Reply

Your email address will not be published. Required fields are marked *