File Closing Conundrum

User avatar
Don Wells
5StarLounger
Posts: 689
Joined: 27 Jan 2010, 16:45
Location: Ottawa, Ontario, Canada

File Closing Conundrum

Post by Don Wells »

    I have a workbook which caused problems when the Hilite Filter.xla was installed. The following code placed in the ThisWorkbook module of the problematic workbook resolves the problem. Until Line 40 was added, the closing process was as follows:
  • User enters File > Close
  • System responds Do you want to save the changes ...
  • User clicks No
  • Workbook remains open
  • User enters File > Close
  • System responds Do you want to save the changes ...
  • User clicks No
  • Workbook closes
Can someone explain why two attempts at closing are necessary without line 40?

Code: Select all

Option Explicit
 Sub Workbook_BeforeClose(Cancel As Boolean)
10        On Error Resume Next
20        AddIns("Filter Highlighter").Installed = True
30        On Error GoTo 0
40        ThisWorkbook.Close
End Sub

Private Sub Workbook_Open()
    On Error Resume Next
    AddIns("Filter Highlighter").Installed = False
    On Error GoTo 0
End Sub
T.I.A.
Regards
Don

User avatar
HansV
Administrator
Posts: 78548
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: File Closing Conundrum

Post by HansV »

I don't have a real explanation, but it's not specific to the Filter Highlighter add-in. Activating any add-in in the Workbook_BeforeClose event causes the two-step closing.
Best wishes,
Hans

User avatar
sdckapr
3StarLounger
Posts: 392
Joined: 25 Jan 2010, 12:21

Re: File Closing Conundrum

Post by sdckapr »

What happens if you remove the line:
ThisWorkbook.close

The code indicates to me that when you close the workbook, it sends another recursive step to close again, calling the routine again. I am surprised it only takes 2 times to close itself. Theoretically it is an infinite loop....

Steve

User avatar
HansV
Administrator
Posts: 78548
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: File Closing Conundrum

Post by HansV »

If I remove the line ThisWorkbook.Close, but not the line activating the Filter Highlighter add-in (or any other add-in!), I have to click the close button twice to close the workbook, as described by Don.
Best wishes,
Hans