Logo 
Search:

MS Office Forum

Ask Question   UnAnswered
Home » Forum » MS Office       RSS Feeds

application.quit - code continues to run

  Asked By: Nicole    Date: Oct 26    Category: MS Office    Views: 1758
  

I am curious about the application.quit command. As I understand it,
in Excel 2003, when my vba code runs into application.quit, it should
immediately go to the auto_close (if any) and then shut down Excel -
however, when I'm using it, it continues to run through my
subroutine - see simplified example below:

Sub test()
Application.Quit
MsgBox ("Test Message 1!")
MsgBox ("Test Message 2!")
MsgBox ("Test Message 3!")
MsgBox ("Test Message 4!")
MsgBox ("Test Message 5!")
MsgBox ("Test Message 6!")
MsgBox ("Test Message 7!")
End Sub

When I run this, I get 7 message boxes that come up - then Excel
quits. However, if I step through the code, Excel quits after the
first line.

Is this the expected result of an application.quit command?

Share: 

 

6 Answers Found

 
Answer #1    Answered By: Ibtihaj Akhtar     Answered On: Oct 26

I have been away at a conference and have only just been able to test this.

It seems that the macro is too busy doing quick stuff like message boxes to
process the Application.Quit which is a slow event.

If you put DoEvents in immediately following the Application.Quit, it works as
expected.


Sub test()
Application.Quit
DoEvents
MsgBox ("Test Message 1!")
MsgBox ("Test Message 2!")
MsgBox ("Test Message 3!")
MsgBox ("Test Message 4!")
MsgBox ("Test Message 5!")
MsgBox ("Test Message 6!")
MsgBox ("Test Message 7!")
End Sub


 
Answer #2    Answered By: Leonardo Costa     Answered On: Oct 26

The DoEvents did the trick - thanks a million!

 
Answer #3    Answered By: Camille Garrett     Answered On: Oct 26

I don't think it has anything to do with speed / busy. I don't think
Application.Quit is supposed to have immediate effect. For instance, the
following code  from Microsoft

Sub QuitMSExcel()

'Loop through all open workbooks.
For Each xWkbk In Workbooks

'If the active workbook does not contain this macro, its name is
'different. If this is the case, then
If xWkbk.Name <> ThisWorkbook.Name Then

'Close the workbook and save changes. Note that you
'don't have to save changes if you don't want to. Closing
'the workbook through this code, prevents the Auto_Close
'macros from running.
xWkbk.Close SaveChanges:=True

End If

'Loop to the next workbook. When all workbooks except for this
'workbook are closed, exit the loop.
Next xWkbk

'Quit Microsoft Excel.
Application.Quit

'This line makes sure that an Auto_Close macro in this workbook
'does not run, if it exists.
ThisWorkbook.Close SaveChanges:=True

End Sub

shows a workbook close after the Application.Quit. The Microsoft programmer
clearly expects this to run  before the quit actually happens.

I'd suggest that Application.Quit simply queues the quit command  for action
next time the application  goes inactive.

 
Answer #4    Answered By: Olivia Campbell     Answered On: Oct 26

That is an interesting point - however if that was the intended flow for the
application.quit, then why would it behave one way (immediately going to the
auto_close) when stepping through the code, yet continue through the message
boxes when actually running the code?

Regardless, adding the DoEvents command  after the application.quit makes the
code behave the way I expect it to, which is all I can ask of my computer!!

Thanks all for the input. I am a TM1 programmer (an OLAP tool - basically a
really big and robust pivot table) which uses Excel as its front end and it's
great finding a forum like this to discuss Excel / VBA issues. I hope I can
contribute sometime soon.

 
Answer #5    Answered By: Hariz Burki     Answered On: Oct 26

Are you using excel  2003? Do you have auto_close macros? Per
Microsoft: when you run  a Visual Basic for Applications macro that
contains the command  Application.Quit, and any open workbooks
contain Auto_Close macros, all of the Auto_Close macros run before
Microsoft Excel quits.

 
Answer #6    Answered By: Bien Nguyen     Answered On: Oct 26

Stepping through code  at source code is not an exact science, unfortunately.
If, as I suspect, the quit is being queued until the code becomes inactive,
then it possibly becomes inactive at the end of the debug "step" in order to
accept your next command, and encounters the quit instead.

 
Didn't find what you were looking for? Find more on application.quit - code continues to run Or get search suggestion and latest updates.




Tagged: