PC Review


Reply
Thread Tools Rate Thread

Closing Form and prevention

 
 
=?Utf-8?B?Q19Bc2NoZW1hbg==?=
Guest
Posts: n/a
 
      23rd Feb 2007
Here is the code I have:

Private Sub Workbook_BeforeClose(Cancel As Boolean)
Application.EnableEvents = False
MsgBox "You have clicked the X in the upper right corner!"
MsgBox "This will close the Excel Program, and all subsequent workbooks
opened within."
MsgBox "If this was an accident, or you do not want to close all the
workbooks then click on the CANCEL button."
MsgBox "Otherwise click the OK button, and everything will be closed.",
vbOKCancel
If vbOK = 1 Then Exit Sub
If vbOK = 0 Then
Application.Quit
End If
End Sub

This partially does what I am wanting it to do. The purpose of this is
if one of the users has multiple workbooks open, and accidentally hits one of
the x's in the corner that it gives them a message of what they did. Then
Depending whether they hit cancel or ok decides the outcome. If they didn't
mean to hit the x, and want to keep the form open then they hit the cancel
button. If they do want to close all of it down then they hit ok. The ok part
works as intended obviously. The problem is if they hit the cancel button
they get the save dialog box. If they hit yes they save it. If they hit no
even though they don't want to close the application it does anyway. With
cancel all remains open.
Is there a way to prevent the file save dialog from popping up, and to
prevent the application from closing after they hit the cancel button?

Thanks,

C_Ascheman
 
Reply With Quote
 
 
 
 
Dave Peterson
Guest
Posts: n/a
 
      23rd Feb 2007
This kind of code is related to a single workbook. It'll fire whenever the user
is closing that workbook--not just when the user clicks on the X to close the
application.

I don't think that there's anything you can do that only fires when the user
clicks on the X to close the application.


C_Ascheman wrote:
>
> Here is the code I have:
>
> Private Sub Workbook_BeforeClose(Cancel As Boolean)
> Application.EnableEvents = False
> MsgBox "You have clicked the X in the upper right corner!"
> MsgBox "This will close the Excel Program, and all subsequent workbooks
> opened within."
> MsgBox "If this was an accident, or you do not want to close all the
> workbooks then click on the CANCEL button."
> MsgBox "Otherwise click the OK button, and everything will be closed.",
> vbOKCancel
> If vbOK = 1 Then Exit Sub
> If vbOK = 0 Then
> Application.Quit
> End If
> End Sub
>
> This partially does what I am wanting it to do. The purpose of this is
> if one of the users has multiple workbooks open, and accidentally hits one of
> the x's in the corner that it gives them a message of what they did. Then
> Depending whether they hit cancel or ok decides the outcome. If they didn't
> mean to hit the x, and want to keep the form open then they hit the cancel
> button. If they do want to close all of it down then they hit ok. The ok part
> works as intended obviously. The problem is if they hit the cancel button
> they get the save dialog box. If they hit yes they save it. If they hit no
> even though they don't want to close the application it does anyway. With
> cancel all remains open.
> Is there a way to prevent the file save dialog from popping up, and to
> prevent the application from closing after they hit the cancel button?
>
> Thanks,
>
> C_Ascheman


--

Dave Peterson
 
Reply With Quote
 
=?Utf-8?B?Q19Bc2NoZW1hbg==?=
Guest
Posts: n/a
 
      23rd Feb 2007
Any suggestions then. Personally I don't think it needs in there, but my boss
disagrees as he has accidentally closed down multiple workbooks by
accidentally clicking the x of the excel window. I will be putting this code
into all the Excel programs we use, which will be a nuisance, but I don't
have much of an option there. Got to do what the boss wants. If you have any
suggestions to help me achieve what I am trying to do then please help.

Thanks,

C_Ascheman



"Dave Peterson" wrote:

> This kind of code is related to a single workbook. It'll fire whenever the user
> is closing that workbook--not just when the user clicks on the X to close the
> application.
>
> I don't think that there's anything you can do that only fires when the user
> clicks on the X to close the application.
>
>
> C_Ascheman wrote:
> >
> > Here is the code I have:
> >
> > Private Sub Workbook_BeforeClose(Cancel As Boolean)
> > Application.EnableEvents = False
> > MsgBox "You have clicked the X in the upper right corner!"
> > MsgBox "This will close the Excel Program, and all subsequent workbooks
> > opened within."
> > MsgBox "If this was an accident, or you do not want to close all the
> > workbooks then click on the CANCEL button."
> > MsgBox "Otherwise click the OK button, and everything will be closed.",
> > vbOKCancel
> > If vbOK = 1 Then Exit Sub
> > If vbOK = 0 Then
> > Application.Quit
> > End If
> > End Sub
> >
> > This partially does what I am wanting it to do. The purpose of this is
> > if one of the users has multiple workbooks open, and accidentally hits one of
> > the x's in the corner that it gives them a message of what they did. Then
> > Depending whether they hit cancel or ok decides the outcome. If they didn't
> > mean to hit the x, and want to keep the form open then they hit the cancel
> > button. If they do want to close all of it down then they hit ok. The ok part
> > works as intended obviously. The problem is if they hit the cancel button
> > they get the save dialog box. If they hit yes they save it. If they hit no
> > even though they don't want to close the application it does anyway. With
> > cancel all remains open.
> > Is there a way to prevent the file save dialog from popping up, and to
> > prevent the application from closing after they hit the cancel button?
> >
> > Thanks,
> >
> > C_Ascheman

>
> --
>
> Dave Peterson
>

 
Reply With Quote
 
=?Utf-8?B?VG9tIE9naWx2eQ==?=
Guest
Posts: n/a
 
      23rd Feb 2007
Leave it in there. Everytime they try to close a workbook, they will get the
prompt. After a while, they will ask you to take it out.

You had some errors in your code.

You test
if vbOK = 1 then

vbOK will always equal 1 - it is a constant. YOu want to examine the
response the user has given.

Private Sub Workbook_BeforeClose(Cancel As Boolean)
Application.EnableEvents = False
MsgBox "You have clicked the X in the upper right corner!"
MsgBox "This will close the Excel Program, and all subsequent workbooks
opened within."
MsgBox "If this was an accident, or you do not want to close all the
workbooks then click on the CANCEL button."
ans = MsgBox( "Otherwise click the OK button, and everything will be
closed.",
vbOKCancel)
If ans = vbOK Then
Exit Sub
else
cancel = True
End If
End Sub



--
Regards,
Tom Ogilvy


"C_Ascheman" wrote:

> Any suggestions then. Personally I don't think it needs in there, but my boss
> disagrees as he has accidentally closed down multiple workbooks by
> accidentally clicking the x of the excel window. I will be putting this code
> into all the Excel programs we use, which will be a nuisance, but I don't
> have much of an option there. Got to do what the boss wants. If you have any
> suggestions to help me achieve what I am trying to do then please help.
>
> Thanks,
>
> C_Ascheman
>
>
>
> "Dave Peterson" wrote:
>
> > This kind of code is related to a single workbook. It'll fire whenever the user
> > is closing that workbook--not just when the user clicks on the X to close the
> > application.
> >
> > I don't think that there's anything you can do that only fires when the user
> > clicks on the X to close the application.
> >
> >
> > C_Ascheman wrote:
> > >
> > > Here is the code I have:
> > >
> > > Private Sub Workbook_BeforeClose(Cancel As Boolean)
> > > Application.EnableEvents = False
> > > MsgBox "You have clicked the X in the upper right corner!"
> > > MsgBox "This will close the Excel Program, and all subsequent workbooks
> > > opened within."
> > > MsgBox "If this was an accident, or you do not want to close all the
> > > workbooks then click on the CANCEL button."
> > > MsgBox "Otherwise click the OK button, and everything will be closed.",
> > > vbOKCancel
> > > If vbOK = 1 Then Exit Sub
> > > If vbOK = 0 Then
> > > Application.Quit
> > > End If
> > > End Sub
> > >
> > > This partially does what I am wanting it to do. The purpose of this is
> > > if one of the users has multiple workbooks open, and accidentally hits one of
> > > the x's in the corner that it gives them a message of what they did. Then
> > > Depending whether they hit cancel or ok decides the outcome. If they didn't
> > > mean to hit the x, and want to keep the form open then they hit the cancel
> > > button. If they do want to close all of it down then they hit ok. The ok part
> > > works as intended obviously. The problem is if they hit the cancel button
> > > they get the save dialog box. If they hit yes they save it. If they hit no
> > > even though they don't want to close the application it does anyway. With
> > > cancel all remains open.
> > > Is there a way to prevent the file save dialog from popping up, and to
> > > prevent the application from closing after they hit the cancel button?
> > >
> > > Thanks,
> > >
> > > C_Ascheman

> >
> > --
> >
> > Dave Peterson
> >

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
duplicate prevention in form chynewalker Microsoft Access Form Coding 2 30th Mar 2010 03:14 PM
Re: Data Execution Prevention keeps closing IE8 Robert Aldwinckle Windows Vista Performance 1 27th Jul 2009 01:51 AM
"My documents" closing [Data execution prevention]? =?Utf-8?B?bGVhZnlsZWE=?= Windows XP Help 0 1st Feb 2006 07:48 AM
Closing event in a MID Child form I don't know if the child form is closing or the main form is closing **Developer** Microsoft C# .NET 1 19th Oct 2005 04:51 PM
Multiple OutputTo (Called on Closing Form) Fails on Closing Database John Andrews Microsoft Access Macros 3 21st May 2004 08:54 AM


Features
 

Advertising
 

Newsgroups
 


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