How do I get Application Events in AddIns?


H

Hollomey

Hi,

I wrote a MsProject-AddIn to do special things.
Now, I must execute some code whenever the active project is changed.

I wrote the following code and put it into the connect module, but it does
not work:


Public WithEvents App1 As Application

Private Sub App1_ProjectBeforeClose(ByVal pj As MSProject.Project, Cancel As
Boolean)
msgbox "Before Close Event"
End Sub


I get the following Compilation Error:

"Fehler beim Kompilieren: Das Objekt löst keine
Automatisierungsereignisse aus"
("Error when compiling: The object does not produce automation events")


Is there somebody who can help?
Thanks
Florian
 
Ad

Advertisements

G

Gérard Ducouret

Hello Hollomey
In the Project Help I found (and tried it : it works !) :

Before you can use events with the Application object, you must create a new
class module and declare an object of type Application with events. For
example, assume that a new class module is created and called
EventClassModule. The new class module contains the following code.

Public WithEvents App As ApplicationAfter the new object has been declared
with events, it appears in the Object drop-down list box in the class
module, and you can write event procedures for the new object. (When you
select the new object in the Object box, the valid events for that object
are listed in the Procedure drop-down list box.)

Before the procedures will run, however, you must connect the declared
object in the class module with the Application object. You can do this with
the following code from any module.

Dim X As New EventClassModule

Sub InitializeApp()
Set X.App = Application
End SubAfter you run the InitializeApp procedure, the App object in the
class module points to the Microsoft Project Application object, and the
event procedures in the class module will run when the events occur.

Hope this help,

Gérard Ducouret
 
H

Hollomey

Hello Gerard

you are right: this works whithin MsProject as VBA code

But how can I get this to work within an MsProject-AddIn?


When I create a new class module in my MsProject-AddIn envirionment and
write the following code within the ConnectModule (Designer)

Public WithEvents App1 As Application

There wont be any events offered in the object drop-down list.


And the code

Private Sub App1_ProjectBeforeClose(ByVal pj As MSProject.Project,
Cancel As Boolean)
MsgBox "Before Close Event"
End Sub

will produce a compilation error!


What DOES work is this:

Public WithEvents proj As Project


So, I do get Project events, but I do not get Application events -- and I
have to do some piece of code whenever the active Project is switched...

Do you have an idea?
Thanks
Florian
 
G

Gérard Ducouret

Hello Florian,
I'm not sure that I understand everything but what runs well is :

The "Public WithEvents App As Application" statement is in the
EventClassModule.

The following Sub is in an ordinary module:

Dim X As New EventClassModule
Sub InitializeApp()
Set X.App = Application
End Sub

The events like...

Private Sub Project_BeforeSave(ByVal pj As Project)
MsgBox "This is BeforeSave !"
End Sub

....are in "ThisProject" (Double Click) where you find the drop down lists
for all the events. Select "Project" in the left side drop down list, and
the events in the right one.

Hope this help,
Gérard Ducouret
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top