Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Excel / Excel Errors / January 2004

Tip: Looking for answers? Try searching our database.

Excel crashes when using Workbook_BeforeClose

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Torsten Hannerfeldt - 08 Jan 2004 08:39 GMT
Hello!
I have a workbook where I don't want any changes to be
saved. (It is only used to communicate with a database).

I use the Workbook_BeforeClose to close the file without
saving. It works fine when only this workbook is open but
Excel crashes when there is another workbook(s) open.

The code:

Private Sub Workbook_BeforeClose(Cancel As Boolean)
 Me.Close SaveChanges:=False
End Sub

Any suggestions?

/Torsten Hannerfeldt
Jan Karel Pieterse - 08 Jan 2004 09:22 GMT
Hi Torsten,

> Private Sub Workbook_BeforeClose(Cancel As Boolean)
>   Me.Close SaveChanges:=False
> End Sub

This code causes the before_close event to fire over and over again
(try stepping through the code by putting a breakpoint on the me.close
line and trying to close the workbook).

Change it to:

Private Sub Workbook_BeforeClose(Cancel As Boolean)
  Me.Saved=True
End Sub

Regards,

Jan Karel Pieterse
Excel MVP
www.jkp-ads.com
- 08 Jan 2004 10:53 GMT
Thanks for your answer Jan!

But, I did try the Me.Saved = True and it didn't solve
the problem. In fact my code was a bit longer than the
example i posted. Here is it in complete:

Private Sub Workbook_BeforeClose(Cancel As Boolean)
Static blnRunning As Boolean

If blnRunning = True Then Exit Sub

 blnRunning = True

 Me.Saved = True
 Me.Activate
 Me.Close SaveChanges:=False
 blnRunning = False
End Sub

I have a static variable tha exits the procedure if it is
already running.

I also tried to include the Me.Activate to be shure the
workbook is activated.

Any suggestions?
/Torsten Hannerfeldt

>-----Original Message-----
>Hi Torsten,
[quoted text clipped - 20 lines]
>
>.
Jan Karel Pieterse - 08 Jan 2004 12:01 GMT
Hi Thorsten,

Strange, the example I gave you should suffice (works fine for me, even
with 6 workbooks open:

Private Sub Workbook_BeforeClose(Cancel As Boolean)
Me.Saved = True
End Sub

Regards,

Jan Karel Pieterse
Excel MVP
www.jkp-ads.com

> But, I did try the Me.Saved = True and it didn't solve
> the problem. In fact my code was a bit longer than the
[quoted text clipped - 15 lines]
> I have a static variable tha exits the procedure if it is
> already running.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.