LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #6   Report Post  
Posted to microsoft.public.excel.programming
alf alf is offline
external usenet poster
 
Posts: 5
Default close workbook event

On 29 nov, 22:39, mcolson wrote:
On Nov 29, 12:08*pm, alf wrote:



On 29 nov, 18:25, mcolson wrote:


On Nov 28, 1:07*pm, JLGWhiz wrote:


Did you have Cancel = True in your event macro? *If Cancel is set to true by
the macro, then when the user presses cancel, the close operation is
terminated.


"mcolson" wrote:
I would like to have an event that runs when a workbook is closed if
the save button is pressed. *I was trying to run the macro with a
Workbook_BeforeClose event, but this wasn't working for me. *If the
user hits cancel, the macro still runs. *I would be fine with that if
I could easily reverse the action because the cancel button was
pressed. *Does anyone have any suggestions. *Basicially, the macro
should run when actually closing the workbook. *If cancel is pressed,
it should not run. *I tried using the workbook deactivate event, but
the macro was running if I switched to another workbook.


No, I do not have any conditions that set cancel = true. *Cancel is
set to true though automatically via Microsoft if the user presses
cancel. *I debugged the code by stepping through it and noticed the
code is ran before the user has the chose of clicking cancel.


The last event fired is Workbook_deactivate.
You must put your code on it.
In order to know that you quit excel, you must set a boolean to True
in the Before_Close Event
then test it in the workbook_deactivate event


I tried this. *When I open a new workbook, the macro ends up running
on the new workbook.


Try this (excel 2003).

ThisWorkbook Section:
---------------------------------
Private Sub Workbook_BeforeClose(cancel As Boolean)
WB_CLOSE
End Sub

Private Sub Workbook_Deactivate()
WB_DEACTIVATE
End Sub

In a Module:
-----------------
Dim bQuit As Boolean

Sub WB_CLOSE()
bQuit = True
End Sub

Sub WB_DEACTIVATE()
If bQuit = True Then
'Excel quit!
'Put your code for excel quit only
MsgBox "excel quit"
Else
'Put your code for deactivate event only
MsgBox "deactivate event"
End If
End Sub

-----
But there is a problem : :
* If you click cancel when you want to exit, bQuit is set to True,
but if you cancel you want bQuit = False...

You could reset bQuit to false in others events (workbook_activate,
worksheet_activate, worksheet_deactivate, selection_change), but if
just after cancellation, you switch to another workbook, the
workbook_deactivate is executed with bQuit = True...

To avoid this, you can try "ontime" excel/vba function to reset bQuit
to false a few seconds after the execution of the Before_Close Event.

And the code modified would be:

ThisWorkbook Section:
---------------------------------
Private Sub Workbook_BeforeClose(cancel As Boolean)
WB_CLOSE
End Sub

Private Sub Workbook_Deactivate()
WB_DEACTIVATE
End Sub

In a Module:
-----------------
Dim bQuit As Boolean

Sub WB_CLOSE()
Dim dTime As Date
bQuit = True
'Then reset bQuit in 2 seconds
dTime = Now + TimeValue("00:00:02")
Application.OnTime dTime, "ResetQuit"
End Sub

Sub WB_DEACTIVATE()
If bQuit = True Then
'Excel quit!
'Put your code for excel quit only
MsgBox "excel quit"
Else
'Put your code for deactivate event only
MsgBox "deactivate event"
End If
End Sub

Private Sub ResetQuit()
'MsgBox "reset bQuit"
bQuit = False
End Sub

-----
Try it, this example works fine for me.
I hope ou understand my explanations (I'm french, and sometimes it is
difficult to explain things in english)

Best regards
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Userform in Before close event of workbook Raj[_2_] Excel Programming 4 June 30th 08 11:29 PM
Workbook-Close StopTimer event Stonewall Rubberbow Excel Discussion (Misc queries) 2 January 12th 08 11:06 PM
Workbook close event kpriyac[_2_] Excel Programming 3 August 18th 06 07:20 PM
Excel WorkBook Close Event Xcelion Excel Programming 2 January 27th 06 03:29 PM
Workbook Close Event Stefano Condotta Excel Programming 1 June 16th 04 10:44 PM


All times are GMT +1. The time now is 04:24 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"