LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Workbook_BeforeClose event and OnTime method

I've got a workbook that should only be closed if the value of a certain cell
is not equal to 2. I've been trying to use the BeforeClose event and an
OnTime method to do it. My code in the ThisWorkbook module is:


Private Sub Workbook_BeforeClose(Cancel as Boolean)
If (Worksheets("Sheet1").Range("F2").Value = 2) Then
Application.OnTime Now() + TimeValue("00:00:05"), "TryToClose", _
Schedule:=True
Cancel = True
End If
End Sub


If cell F2 has a value of 2, the macro cancels the save, waits five seconds,
and calls TryToClose.


Sub TryToClose()
ThisWorkbook.Close
End Sub


TryToClose then attempts to close the workbook (which activates the
Workbook_BeforeClose event again). This should continue with one macro
calling the other (and vice versa) until the value of F2 is no longer 2.
However, the loop only works once. It appears that when TryToClose calls the
BeforeClose event, the OnTime method does not work properly. I'm not sure
why.

Any help would be very much appreciated.

--
Bejaz
 
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
Workbook_BeforeClose event is not canceled Mircea Pleteriu[_2_] Excel Programming 1 June 8th 05 08:47 AM
Detecting a cancel on Workbook_BeforeClose event? Don Wiss Excel Programming 3 January 18th 05 12:49 PM
Workbook_Beforeclose vs BeforeClose Event Juan Pablo González Excel Programming 3 February 2nd 04 12:17 AM
Workbook_BeforeClose Event Shatin Excel Programming 2 January 24th 04 03:50 AM
OnTime event not firing in Workbook_Open event procedure GingerTommy Excel Programming 0 September 24th 03 03:18 PM


All times are GMT +1. The time now is 06:54 PM.

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"