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?
 

xenou

MrExcel MVP, Moderator
Joined
Mar 2, 2007
Messages
16,556
Office Version
2013
Platform
Windows
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 ...

ξ
 

ClimoC

Well-known Member
Joined
Aug 21, 2009
Messages
584
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!
 

xenou

MrExcel MVP, Moderator
Joined
Mar 2, 2007
Messages
16,556
Office Version
2013
Platform
Windows
Ah, I see - Microsoft Project. Glad you figured it out!
 

ClimoC

Well-known Member
Joined
Aug 21, 2009
Messages
584
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.
 

xenou

MrExcel MVP, Moderator
Joined
Mar 2, 2007
Messages
16,556
Office Version
2013
Platform
Windows
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:

Forum statistics

Threads
1,082,641
Messages
5,366,713
Members
400,915
Latest member
NFlem

Some videos you may like

This Week's Hot Topics

Top