Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 23
Default "Excel has encountered a problem..." error

I am trying to force closure of an Excel 2003 workbook via VBA behind a user
form rather than with the normal File | Close or X and have tried several
different approaches; none of which solves the problem. When the user form
is hidden, the File | Save and X close options are available and if selected
cause a fatal crash. I want to retain the ability to save and close normally
for program maintenance, but want to present a normal user with a message and
process to exit the program via the user form. I have tried code in
Workbook_BeforeClose, but it seems that the routine gets called twice. It
works as expected the second time through. The code I'm using is:

Private Sub Workbook_BeforeClose(Cancel As Boolean)
'Check to see if frmEntry is loaded
If isFormLoaded(frmEntry) Then
MsgBox "You must close the application via the Exit button on the
Entry form."
'Restore form
frmEntry.Show
End If

End Sub

It calls the function below:

Function isFormLoaded(frmName As Object) As Boolean
Dim intI As Integer

'initialize the return value to false
isFormLoaded = False

'loop through all open forms
For intI = 0 To UserForms.Count - 1
'test to see if the current form has the
'same name as the frmName
If UserForms(intI) Is frmName Then
'return true
isFormLoaded = True
'exit the loop
Exit For
End If
Next
End Function

Any help you can provide with this code or any other options will be
sincerely appreciated.

Thanks...
  #2   Report Post  
Posted to microsoft.public.excel.programming
Jay Jay is offline
external usenet poster
 
Posts: 671
Default "Excel has encountered a problem..." error

Hi Hawk -

Try adding the statement:

Cancel = True

immediately before the comment statement:
'Restore form

in your Workbook_BeforeClose procedure.

--
Jay


"The Hawk" wrote:

I am trying to force closure of an Excel 2003 workbook via VBA behind a user
form rather than with the normal File | Close or X and have tried several
different approaches; none of which solves the problem. When the user form
is hidden, the File | Save and X close options are available and if selected
cause a fatal crash. I want to retain the ability to save and close normally
for program maintenance, but want to present a normal user with a message and
process to exit the program via the user form. I have tried code in
Workbook_BeforeClose, but it seems that the routine gets called twice. It
works as expected the second time through. The code I'm using is:

Private Sub Workbook_BeforeClose(Cancel As Boolean)
'Check to see if frmEntry is loaded
If isFormLoaded(frmEntry) Then
MsgBox "You must close the application via the Exit button on the
Entry form."
'Restore form
frmEntry.Show
End If

End Sub

It calls the function below:

Function isFormLoaded(frmName As Object) As Boolean
Dim intI As Integer

'initialize the return value to false
isFormLoaded = False

'loop through all open forms
For intI = 0 To UserForms.Count - 1
'test to see if the current form has the
'same name as the frmName
If UserForms(intI) Is frmName Then
'return true
isFormLoaded = True
'exit the loop
Exit For
End If
Next
End Function

Any help you can provide with this code or any other options will be
sincerely appreciated.

Thanks...

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 23
Default "Excel has encountered a problem..." error

Thanks Jay. Unfortunately that did not fix the problem as the message and
form still spawn a second time.

Any other advice will be appreciated.

"The Hawk" wrote:

I am trying to force closure of an Excel 2003 workbook via VBA behind a user
form rather than with the normal File | Close or X and have tried several
different approaches; none of which solves the problem. When the user form
is hidden, the File | Save and X close options are available and if selected
cause a fatal crash. I want to retain the ability to save and close normally
for program maintenance, but want to present a normal user with a message and
process to exit the program via the user form. I have tried code in
Workbook_BeforeClose, but it seems that the routine gets called twice. It
works as expected the second time through. The code I'm using is:

Private Sub Workbook_BeforeClose(Cancel As Boolean)
'Check to see if frmEntry is loaded
If isFormLoaded(frmEntry) Then
MsgBox "You must close the application via the Exit button on the
Entry form."
'Restore form
frmEntry.Show
End If

End Sub

It calls the function below:

Function isFormLoaded(frmName As Object) As Boolean
Dim intI As Integer

'initialize the return value to false
isFormLoaded = False

'loop through all open forms
For intI = 0 To UserForms.Count - 1
'test to see if the current form has the
'same name as the frmName
If UserForms(intI) Is frmName Then
'return true
isFormLoaded = True
'exit the loop
Exit For
End If
Next
End Function

Any help you can provide with this code or any other options will be
sincerely appreciated.

Thanks...

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 23
Default "Excel has encountered a problem..." error

To clarify, the message and form are now in a loop and you cannot exit the
Workbook or Excel without entering the VBA code and commenting out the
Cancel=True statement.

Thanks...

"The Hawk" wrote:

Thanks Jay. Unfortunately that did not fix the problem as the message and
form still spawn a second time.

Any other advice will be appreciated.

"The Hawk" wrote:

I am trying to force closure of an Excel 2003 workbook via VBA behind a user
form rather than with the normal File | Close or X and have tried several
different approaches; none of which solves the problem. When the user form
is hidden, the File | Save and X close options are available and if selected
cause a fatal crash. I want to retain the ability to save and close normally
for program maintenance, but want to present a normal user with a message and
process to exit the program via the user form. I have tried code in
Workbook_BeforeClose, but it seems that the routine gets called twice. It
works as expected the second time through. The code I'm using is:

Private Sub Workbook_BeforeClose(Cancel As Boolean)
'Check to see if frmEntry is loaded
If isFormLoaded(frmEntry) Then
MsgBox "You must close the application via the Exit button on the
Entry form."
'Restore form
frmEntry.Show
End If

End Sub

It calls the function below:

Function isFormLoaded(frmName As Object) As Boolean
Dim intI As Integer

'initialize the return value to false
isFormLoaded = False

'loop through all open forms
For intI = 0 To UserForms.Count - 1
'test to see if the current form has the
'same name as the frmName
If UserForms(intI) Is frmName Then
'return true
isFormLoaded = True
'exit the loop
Exit For
End If
Next
End Function

Any help you can provide with this code or any other options will be
sincerely appreciated.

Thanks...

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 857
Default "Excel has encountered a problem..." error

When you try to close the workbook from the UserForm, I believe it is still
loaded when the BeforeClose event fires. My suggestion would be to have a
public variable in the Userform that will indicate if it's closing. For
example, I created a user form named frmEntry and have this in the code
module:

Public bFormIsClosing As Boolean

Private Sub UserForm_Initialize()
bFormIsClosing = False
End Sub

Private Sub UserForm_Terminate()
bFormIsClosing = True
ThisWorkbook.Close
End Sub


Then, in the Thisworkbook module, the BeforeClose handler looks like this:

Private Sub Workbook_BeforeClose(Cancel As Boolean)
'Check to see if frmEntry is closing
If Not frmEntry.bFormIsClosing Then
Cancel = True
MsgBox "You must close the application via the Exit button on the
Entry form."
frmEntry.Show
End If
End Sub





--
Hope that helps.

Vergel Adriano


"The Hawk" wrote:

To clarify, the message and form are now in a loop and you cannot exit the
Workbook or Excel without entering the VBA code and commenting out the
Cancel=True statement.

Thanks...

"The Hawk" wrote:

Thanks Jay. Unfortunately that did not fix the problem as the message and
form still spawn a second time.

Any other advice will be appreciated.

"The Hawk" wrote:

I am trying to force closure of an Excel 2003 workbook via VBA behind a user
form rather than with the normal File | Close or X and have tried several
different approaches; none of which solves the problem. When the user form
is hidden, the File | Save and X close options are available and if selected
cause a fatal crash. I want to retain the ability to save and close normally
for program maintenance, but want to present a normal user with a message and
process to exit the program via the user form. I have tried code in
Workbook_BeforeClose, but it seems that the routine gets called twice. It
works as expected the second time through. The code I'm using is:

Private Sub Workbook_BeforeClose(Cancel As Boolean)
'Check to see if frmEntry is loaded
If isFormLoaded(frmEntry) Then
MsgBox "You must close the application via the Exit button on the
Entry form."
'Restore form
frmEntry.Show
End If

End Sub

It calls the function below:

Function isFormLoaded(frmName As Object) As Boolean
Dim intI As Integer

'initialize the return value to false
isFormLoaded = False

'loop through all open forms
For intI = 0 To UserForms.Count - 1
'test to see if the current form has the
'same name as the frmName
If UserForms(intI) Is frmName Then
'return true
isFormLoaded = True
'exit the loop
Exit For
End If
Next
End Function

Any help you can provide with this code or any other options will be
sincerely appreciated.

Thanks...



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 23
Default "Excel has encountered a problem..." error

That was it Vergel! Thanks for your help!

"Vergel Adriano" wrote:

When you try to close the workbook from the UserForm, I believe it is still
loaded when the BeforeClose event fires. My suggestion would be to have a
public variable in the Userform that will indicate if it's closing. For
example, I created a user form named frmEntry and have this in the code
module:

Public bFormIsClosing As Boolean

Private Sub UserForm_Initialize()
bFormIsClosing = False
End Sub

Private Sub UserForm_Terminate()
bFormIsClosing = True
ThisWorkbook.Close
End Sub


Then, in the Thisworkbook module, the BeforeClose handler looks like this:

Private Sub Workbook_BeforeClose(Cancel As Boolean)
'Check to see if frmEntry is closing
If Not frmEntry.bFormIsClosing Then
Cancel = True
MsgBox "You must close the application via the Exit button on the
Entry form."
frmEntry.Show
End If
End Sub





--
Hope that helps.

Vergel Adriano


"The Hawk" wrote:

To clarify, the message and form are now in a loop and you cannot exit the
Workbook or Excel without entering the VBA code and commenting out the
Cancel=True statement.

Thanks...

"The Hawk" wrote:

Thanks Jay. Unfortunately that did not fix the problem as the message and
form still spawn a second time.

Any other advice will be appreciated.

"The Hawk" wrote:

I am trying to force closure of an Excel 2003 workbook via VBA behind a user
form rather than with the normal File | Close or X and have tried several
different approaches; none of which solves the problem. When the user form
is hidden, the File | Save and X close options are available and if selected
cause a fatal crash. I want to retain the ability to save and close normally
for program maintenance, but want to present a normal user with a message and
process to exit the program via the user form. I have tried code in
Workbook_BeforeClose, but it seems that the routine gets called twice. It
works as expected the second time through. The code I'm using is:

Private Sub Workbook_BeforeClose(Cancel As Boolean)
'Check to see if frmEntry is loaded
If isFormLoaded(frmEntry) Then
MsgBox "You must close the application via the Exit button on the
Entry form."
'Restore form
frmEntry.Show
End If

End Sub

It calls the function below:

Function isFormLoaded(frmName As Object) As Boolean
Dim intI As Integer

'initialize the return value to false
isFormLoaded = False

'loop through all open forms
For intI = 0 To UserForms.Count - 1
'test to see if the current form has the
'same name as the frmName
If UserForms(intI) Is frmName Then
'return true
isFormLoaded = True
'exit the loop
Exit For
End If
Next
End Function

Any help you can provide with this code or any other options will be
sincerely appreciated.

Thanks...

  #7   Report Post  
Posted to microsoft.public.excel.programming
Jay Jay is offline
external usenet poster
 
Posts: 671
Default "Excel has encountered a problem..." error

Hi 'Hawk' -

I'm sorry I wasn't able to follow-up in a timely fashion; glad to see you
and Vergel resolved your problem.

--
Jay

"The Hawk" wrote:

That was it Vergel! Thanks for your help!

"Vergel Adriano" wrote:

When you try to close the workbook from the UserForm, I believe it is still
loaded when the BeforeClose event fires. My suggestion would be to have a
public variable in the Userform that will indicate if it's closing. For
example, I created a user form named frmEntry and have this in the code
module:

Public bFormIsClosing As Boolean

Private Sub UserForm_Initialize()
bFormIsClosing = False
End Sub

Private Sub UserForm_Terminate()
bFormIsClosing = True
ThisWorkbook.Close
End Sub


Then, in the Thisworkbook module, the BeforeClose handler looks like this:

Private Sub Workbook_BeforeClose(Cancel As Boolean)
'Check to see if frmEntry is closing
If Not frmEntry.bFormIsClosing Then
Cancel = True
MsgBox "You must close the application via the Exit button on the
Entry form."
frmEntry.Show
End If
End Sub





--
Hope that helps.

Vergel Adriano


"The Hawk" wrote:

To clarify, the message and form are now in a loop and you cannot exit the
Workbook or Excel without entering the VBA code and commenting out the
Cancel=True statement.

Thanks...

"The Hawk" wrote:

Thanks Jay. Unfortunately that did not fix the problem as the message and
form still spawn a second time.

Any other advice will be appreciated.

"The Hawk" wrote:

I am trying to force closure of an Excel 2003 workbook via VBA behind a user
form rather than with the normal File | Close or X and have tried several
different approaches; none of which solves the problem. When the user form
is hidden, the File | Save and X close options are available and if selected
cause a fatal crash. I want to retain the ability to save and close normally
for program maintenance, but want to present a normal user with a message and
process to exit the program via the user form. I have tried code in
Workbook_BeforeClose, but it seems that the routine gets called twice. It
works as expected the second time through. The code I'm using is:

Private Sub Workbook_BeforeClose(Cancel As Boolean)
'Check to see if frmEntry is loaded
If isFormLoaded(frmEntry) Then
MsgBox "You must close the application via the Exit button on the
Entry form."
'Restore form
frmEntry.Show
End If

End Sub

It calls the function below:

Function isFormLoaded(frmName As Object) As Boolean
Dim intI As Integer

'initialize the return value to false
isFormLoaded = False

'loop through all open forms
For intI = 0 To UserForms.Count - 1
'test to see if the current form has the
'same name as the frmName
If UserForms(intI) Is frmName Then
'return true
isFormLoaded = True
'exit the loop
Exit For
End If
Next
End Function

Any help you can provide with this code or any other options will be
sincerely appreciated.

Thanks...

Reply
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
"Excel encountered an error and had to remove some formatting toavoid corrupting the workbook. Please re-check your formatting carefully." Greg Lovern Excel Discussion (Misc queries) 0 July 18th 08 09:42 PM
Error: "Excel encountered an error and had to remove some formatti Carl Excel Discussion (Misc queries) 0 September 18th 06 06:39 PM
can I bypass "Windows has encountered a problem" fredashe Excel Discussion (Misc queries) 0 April 15th 06 04:53 PM
XL "encountered a problem & needs to close" Don Rouse Excel Programming 3 July 19th 05 09:34 PM
"Microsoft Excel has encountered a problem..." Ken Dahlberg Excel Programming 1 July 22nd 03 09:51 PM


All times are GMT +1. The time now is 03:38 PM.

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

About Us

"It's about Microsoft Excel"