BeforeClose problem - Cancel close

ClimoC

Well-known Member
Joined
Aug 21, 2009
Messages
584
I'm not even entirely sure my code will work, but right now I'm getting stuck on something else

"Compile Error:

Procedure declaration does not match descfription of event or procedure having the same name"

Is the error I get when this fires:

Code:
Private Sub Project_BeforeClose(Cancel As Boolean)


Dim answer As String

Dim uname As String
uname = Environ("USERNAME")

answer = MsgBox("This is your only chance to Cancel the Closing of this workbook. After this message, you can only Save&Quit or just Quit", vbYesNo, "Last Chance to Cancel")
If answer = vbNo Then
Cancel = True
GoTo icancelled
Else
End If


If uname <> "ew" Then
Run "eh"
Else
End If

icancelled:

End Sub

No amount of tweaking seems to fix it.

And when it was working before, I had to have it look like thios to do anything:

Code:
Sub Project_BeforeClose(ByVal pj1 As Project)

If (pj1 Is Nothing) Then
End If

'...rest of code

End Sub

What happens is that the sub "eh" fires a userform stored in the global.mpt (same deal as personal.xls) which gives a range of saving options (where, how, filenames, etc).

But I need a "Cancel - I did not mean to close the project..." and I can't seem to work one in.

Any ideas?
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
As far as I can tell, your project must have a before close event in order to use a before close event handler.

It's not easy to see what you are doing - it appears you have created a custom Project class. If so, you must create a Before Close event for it, and raise the event on closing. But if you are trying to capture the Excel workbook before close event then you would need to use the Workbook_BeforeClose event in the ThisWorkbook class module. Edit: note, or maybe you've subclassed the Workbook class? In which case it must be declare WithEvents ...

ξ
 
Upvote 0
Well it's Project, but the vba is identical save for the difference between "Workbook" and "Project"

All I had to do was change it to "ThisProject_BeforeClose"

and it worked!

Thanks xenou!
 
Upvote 0
Ah, I see - Microsoft Project. Glad you figured it out!
 
Upvote 0
LOL - actually, 1 minute after posting that I realised it didn't work. The sub line wasn't picked up as an object.

I did get it working, almost, but to no avail - no-one on the net seems to have found a way to properly 'Cancel' the Close action in VBA. You can either FileClose, not just not write the line.

I might get better luck if I do this as a 'BeforeSave' and change what exactly it's asking for.
 
Upvote 0
I might get better luck if I do this as a 'BeforeSave' and change what exactly it's asking for.

Well, you can't just "change what exactly it's looking for". These event procedures must stay exactly as they are - the name of the procedure and the arguments they are using. The point is that the application's event model is expecting (and requiring) the events as they are named and with the arguments as provided.

I'm not familiar with Project's object model or events, but this article suggests there is a before close event with a cancel option - but you must create an application class to access that event. Note: You're in an Excel forum, BTW ;) I'm sorry that I'm not very familiar with MS Project - I can try to follow along.

BeforeClose (application event):
http://msdn.microsoft.com/en-us/library/aa204891(v=office.11).aspx
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,559
Messages
6,179,515
Members
452,921
Latest member
BBQKING

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