Before Close Event

poleary2000

Active Member
Joined
Apr 1, 2002
Messages
354
I have the following code in ThisWorkbook.

Private Sub Workbook_BeforeClose(Cancel As Boolean)

ExpenseErrors.Hide

Call EnablePrint

End Sub

Here is the code for the sub I am calling - EnablePrint().

Sub EnablePrint()

Dim cb As CommandBar
Dim bb As CommandBarControl

For Each cb In CommandBars
For Each bb In cb.Controls
If Left(bb.Caption, 5) = "Print" Then _
bb.Enabled = True
Next
Next

Application.CommandBars("Worksheet Menu Bar").Controls _
("File").Controls("Print Preview").Enabled = True

Application.CommandBars("Worksheet Menu Bar").Controls _
("File").Controls("Print...").Enabled = True

End Sub

MY PROBLEM -- As you can see in the Before Workbook Close, I am hiding a UserForm call ExpenseErrors before calling the EnablePrint sub. If this UserForm is NOT on the screen, I can close Excel without a problem. However, if the UserForm is on the screen when the user closes, I receive an Error from Excel that states

"MS Excel has encountered and error and must close. Sorry for the incovenience." There is a check box that asks you if you would like to start again.

Again, this ONLY happens if a UserForm is in its Show state when the user tries to close. I thought that adding the ExpenseErrors.Hide would alleviate the problem. Not so.

Any suggestions?
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
If I do NOT use the Call EnablePrint and instead copy and paste the code from the EnablePrint() to the ThisWorkbork.

For Each cb In CommandBars

It says RunTime Error 424 - Object Required and highlights the above section of the code.

I have no idea!
 
Upvote 0
Just a thought, try using the unload command before you close:

unload ExpenseErrors

Not sure if it will work, but worth a try.
 
Upvote 0

Forum statistics

Threads
1,214,642
Messages
6,120,701
Members
448,980
Latest member
CarlosWin

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top