Problem with before close

R

Raj

Hi,

I am having a problem with the code below. The Msgbox appears twice
when I close the workbook Why?
Also, a Microsoft bug report is being generated everytime.


Private Sub Workbook_BeforeClose(Cancel As Boolean)
rspresponse1 = MsgBox("Do you want to Finalise?", vbYesNo)
If rspresponse1 = vbYes Then Me.Close SaveChanges:=True
If rspresponse1 = vbNo Then Me.Close SaveChanges:=False
End Sub

Please help.

Thanks in advance.

Regards,
Raj
 
P

paul.robinson

Hi
You are invoking the close from within the close, which calls the
BeforeClose...hence the message box appears twice and you get an error
rather than an infinite loop. You need to suppress the BeforeClose
event being called again

Private Sub Workbook_BeforeClose(Cancel As Boolean)
rspresponse1 = MsgBox("Do you want to Finalise?", vbYesNo)
Application.EnableEvents = False
If rspresponse1 = vbYes Then Me.Close SaveChanges:=True
If rspresponse1 = vbNo Then Me.Close SaveChanges:=False
Application.EnableEvents = True
End Sub

You need to set it to true at the end as false lasts as long as Excel
is open, not to the end of the sub.
regards
Paul
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top