Application.Quit does not work when ActiveWindow.Visible is False

Goodwin

New Member
Joined
Aug 11, 2010
Messages
13
Here's a real head scratcher...Within the BeforeClose event, I call one of two procedures depending on whether the workbook is the only workbook in that particular instance of Excel. The first works fine. The second ends up working exactly like the first despite the use of Application.Quit. So, I end up having to click Excel's (the app's not the wb's) close button twice to get the desired effect. Any explanation would be greatly appreciated.

Code:
Sub CloseWorkbook()
ThisWorkbook.Windows(1).Visible = False
ThisWorkbook.Saved = True
ThisWorkbook.Windows(1).Close False
End Sub
 
Sub CloseWorkbookAndExcel()
ThisWorkbook.Windows(1).Visible = False
ThisWorkbook.Saved = True
Application.Quit
End Sub
 
It works for me, though in an otherwise blank workbook. Though interesting the before_close code runs twice when the application is exited. I still think this is partially dead code - hiding the window and then closing without saving changes accomplishes nothing different than just closing the workbook (?). It's a puzzler, anyway. Could we hide the window on opening instead? I'm not sure how to do that though.
 
Upvote 0

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Jaafar...didn't mean to ignore your suggestion. I tried employing OnTime, and it didn't seem to make a difference. Hopefully you have a chance to read my latest post (Post #9) detailing where I stand now. Bottom line, I know I'm being a little picky (in fact, in just came to my attention that Excel 2007 essentially acts the way I'm trying to avoid), but with how much time I've already invested in this, I can't let it go.


Try this it should work :

Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
 
    Application.OnTime Now, Me.CodeName & ".CloseNow"
 
End Sub
 
Private Sub CloseNow()
 
   Windows(ThisWorkbook.Name).Visible = False

   Select Case Application.Workbooks.Count

      Case Is > 1
         ThisWorkbook.Saved = True
         ThisWorkbook.Close SaveChanges:=False
      Case Is = 1
         ThisWorkbook.Saved = True
         Application.Quit

   End Select

End Sub
 
Upvote 0
Jaafar...brilliant. Turns out you were right all along about taking advantage of the OnTime method. Once the rest of the code was righted, the OnTime workaround fixed everything on my end. Why does this workaround not cause the same issues xenou and I were noticing (i.e., BeforeClose running twice)?

Thanks to both of you. You're awesome.
 
Upvote 0
Why does this workaround not cause the same issues xenou and I were noticing (i.e., BeforeClose running twice)?
QUOTE]

I guess it's because when you schedule an OnTime Procedure , the workbook stays in memory even after exiting the BeforeClose event and closing the workbook. When the scheduled time arrives, the workbook is reloaded and the Application.Quit statement gets executed. so in other words it's like hitting the Excel X Closing button after the workbook is closed but via code.
 
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,176
Members
451,543
Latest member
cesymcox

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top