Home |
Search |
Today's Posts |
|
#1
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Can someone please explain why Application.Quit is ignored
here and VBA goes on working? This is on Excel 97. My code is similar to this: Sub My_subrtn() Application.DisplayAlerts = False Dim x As Integer x = 3 If x 2 Then Application.Quit End If MsgBox "VBA is still running" MsgBox "VBA is still running" MsgBox "VBA is still running" End Sub Here, Application.Quit is ignored(?) and all the MsgBoxes are executed. I googled this and found that a DoEvents after the Application.Quit statement solves the problem. Does anyone know why? I ask because I always thought that each statement in the program must be executed before the next statement enters the program. The above behavior throws that out the window. In this case for example, do I need a DoEvents statement after x = 3 in order to make sure the If statement doesn't execute first? In other words what is it that makes Application.Quit different? Thank you for your time. Andy |
#2
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Op 16-4-2011 19:41, Andy schreef:
Can someone please explain why Application.Quit is ignored here and VBA goes on working? This is on Excel 97. My code is similar to this: Sub My_subrtn() Application.DisplayAlerts = False Dim x As Integer x = 3 If x 2 Then Application.Quit End If MsgBox "VBA is still running" MsgBox "VBA is still running" MsgBox "VBA is still running" End Sub Here, Application.Quit is ignored(?) and all the MsgBoxes are executed. I googled this and found that a DoEvents after the Application.Quit statement solves the problem. Does anyone know why? I ask because I always thought that each statement in the program must be executed before the next statement enters the program. The above behavior throws that out the window. In this case for example, do I need a DoEvents statement after x = 3 in order to make sure the If statement doesn't execute first? In other words what is it that makes Application.Quit different? Thank you for your time. Andy Hi Andy, Not realy shore why the subroutine is continued, but i think it's because excel wait for the subroutine to be finished before it closes. maybe you shoot ad Exit Sub after the Application.Quit .... Sub My_subrtn() Application.DisplayAlerts = False Dim x As Integer x = 3 If x 2 Then Application.Quit Exit Sub End If MsgBox "VBA is still running" MsgBox "VBA is still running" MsgBox "VBA is still running" End Sub Gerard |
#3
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
On Mon, 18 Apr 2011 11:31:58 +0200, GerardV
wrote: Hi Andy, Not realy shore why the subroutine is continued, but i think it's because excel wait for the subroutine to be finished before it closes. maybe you shoot ad Exit Sub after the Application.Quit .... Sub My_subrtn() Application.DisplayAlerts = False Dim x As Integer x = 3 If x 2 Then Application.Quit Exit Sub End If MsgBox "VBA is still running" MsgBox "VBA is still running" MsgBox "VBA is still running" End Sub Gerard Thanks for the reply Gerard. Even if you Exit Sub after the Application.Quit, VBA is still running. Here is your subroutine with a minor modification - This is on Excel 2000: Sub My_subrtn() Application.DisplayAlerts = False Dim x As Integer x = 3 If x 2 Then Call JustQuit End If MsgBox "VBA is still running" MsgBox "VBA is still running" MsgBox "VBA is still running" End Sub Sub JustQuit() Application.Quit End Sub Again, thanks for the reply. Andy |
#4
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Try closing all workbooks before calling quit.
Something as simple as... Workbooks.Close Application.Quit If you are automating Excel, then you have to call quit on the object reference not "Application". -- Jim Cone Portland, Oregon USA . http://www.mediafire.com/PrimitiveSoftware . (Bingo Card Variety, Excel workbook - in the free folder) |
#5
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
On Tue, 19 Apr 2011 21:16:31 -0700, "Jim Cone"
wrote: Try closing all workbooks before calling quit. Something as simple as... Workbooks.Close Application.Quit If you are automating Excel, then you have to call quit on the object reference not "Application". Good tip - thank you. To make it work I ended up closing the workbooks one by one - something like: ActiveWorkbook.Close Savechanges:=False Workbooks("Name_01.xls").Close Savechanges:=False (Still working on this - the last workbook for some reason is not closing) As an aside, I always thought that "Application.Quit" will close all workbooks no questions asked. Again, Thank you. Andy |
#6
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
"Still working on this - the last workbook for some reason is not closing"
Corrupted workbooks are a problem and what they will do to workbook code is unpredictable. I believe having multiple versions of Excel on your machine can help identify problems. Sometimes that smoothly running piece of code has difficulties in another version. (even if the "other" version will not be used to run the finished program, it pays to check) -- Jim Cone Portland, Oregon USA . http://www.mediafire.com/PrimitiveSoftware . (Extras for Excel add-in: convenience built-in) "Andy" wrote in message ... On Tue, 19 Apr 2011 21:16:31 -0700, "Jim Cone" wrote: Try closing all workbooks before calling quit. Something as simple as... Workbooks.Close Application.Quit If you are automating Excel, then you have to call quit on the object reference not "Application". Good tip - thank you. To make it work I ended up closing the workbooks one by one - something like: ActiveWorkbook.Close Savechanges:=False Workbooks("Name_01.xls").Close Savechanges:=False (Still working on this - the last workbook for some reason is not closing) As an aside, I always thought that "Application.Quit" will close all workbooks no questions asked. Again, Thank you. Andy |
#7
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
The behavior of the Application.Quit statement is what it is, and so we
need to work with that! In all probability, Excel has a 'Terminate' event that runs before shutdown occurs. I'm not sure why you have executable code after the Application.Quit statement but that's rather unusual. This is a normal statement for a shutdown routine that also handles menus, toolbar, and UI settings cleanup as well as saving of any open workbooks, BUT this is always the last executable line in the shutdown routine. Also, the shutdown routine might begin with 'On Error Resume Next' since at this point it's too late to mitigate any errors that might occur. Are you actually using this statement in a project OR just messing around with it for curiosity? -- Garry Free usenet access at http://www.eternal-september.org ClassicVB Users Regroup! comp.lang.basic.visual.misc |
#8
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
On Wed, 20 Apr 2011 01:19:34 -0400, GS wrote:
The behavior of the Application.Quit statement is what it is, and so we need to work with that! In all probability, Excel has a 'Terminate' event that runs before shutdown occurs. I'm not sure why you have executable code after the Application.Quit statement but that's rather unusual. This is a normal statement for a shutdown routine that also handles menus, toolbar, and UI settings cleanup as well as saving of any open workbooks, BUT this is always the last executable line in the shutdown routine. In this case the need to shut down is the result of an error in the spreadsheet. If it does not shutdown then it mails itself to a number of users who are now receiving the wrong information. Also, the shutdown routine might begin with 'On Error Resume Next' since at this point it's too late to mitigate any errors that might occur. Are you actually using this statement in a project OR just messing around with it for curiosity? I did not think about 'On Error Resume Next' - I will give it a shot. And no, this is not a curiosity item - I was very surprised to discover that 'Application.Quit' is to some extend ignored by VBA and Excel. I understand that it depends on the Operating System and I am not clear yet on how to deal with this. Thank you for the comments. Andy |
#9
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
After serious thinking Andy wrote :
On Wed, 20 Apr 2011 01:19:34 -0400, GS wrote: The behavior of the Application.Quit statement is what it is, and so we need to work with that! In all probability, Excel has a 'Terminate' event that runs before shutdown occurs. I'm not sure why you have executable code after the Application.Quit statement but that's rather unusual. This is a normal statement for a shutdown routine that also handles menus, toolbar, and UI settings cleanup as well as saving of any open workbooks, BUT this is always the last executable line in the shutdown routine. In this case the need to shut down is the result of an error in the spreadsheet. If it does not shutdown then it mails itself to a number of users who are now receiving the wrong information. Could you not recode the procedure that does this to abort if the error exists, giving the user an instruction as to how to correct the error maybe. I'm thinking it might be easier to exit a sub/function than code for all the nuances surrounding a shutdown.<g Also, the shutdown routine might begin with 'On Error Resume Next' since at this point it's too late to mitigate any errors that might occur. Are you actually using this statement in a project OR just messing around with it for curiosity? I did not think about 'On Error Resume Next' - I will give it a shot. And no, this is not a curiosity item - I was very surprised to discover that 'Application.Quit' is to some extend ignored by VBA and Excel. I understand that it depends on the Operating System and I am not clear yet on how to deal with this. Thank you for the comments. Andy -- Garry Free usenet access at http://www.eternal-september.org ClassicVB Users Regroup! comp.lang.basic.visual.misc |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
Application.Quit Does not Close the Application | Excel Programming | |||
difference application.quit & application.close | Excel Programming | |||
macro to close excel application other than application.quit | Excel Programming | |||
application.quit will not shut off application | Excel Programming | |||
Quit Application | Excel Programming |