Project_Open Event in a COM Add-in

M

Mark Durrenberger

Has anyone been successful at trapping the On_open event in a com add-in.
I'm having a heck of a time getting this to work.

I created a class
=====
Option Explicit
Private WithEvents ProjEvents As MSProject.Project

Private Sub ProjEvents_Open(ByVal pj As MSProject.Project)
MsgBox "open event", vbOKOnly
End Sub
=====
I know the class is right because when I click on the Procedure window I'm
presented with the
Activate, BeforeClose, BeforePrint,BeforeSave, Calculate,Deactivate, Open
procedures.
I think these are called "Document level" events (as opposed to "Application
level events")

What I don't know how to do is tie these events to the instance of Project.
It does not appear to work the same as application level events.

I have searched high and low for the solution to this problem. I even tried
code that is supposed to work with app level events in a Word COM Add-in.

Any help appreciated,
Thanks,
Mark

BTW, if you want to implement application level events in a COM add in for
MSProject, follow the instructions below.

Application level events are done this way:
Create your class. I'll call it MyClass
in MyClass put
===== Class MyClass ====
Public WithEvents AppEvents as MSProject.Application
along with your event code like

Private Sub AppEvents_ProjectBeforeTaskChange(ByVal tsk As MSProject.Task, _
ByVal Field As PjField, _
ByVal NewVal As Variant, _
Cancel As Boolean)
'do stuff on change
end sub

In a regular module, create a public object variable
Public oAppEvents as MyClass

Somewhere in your code (I do this in the IDTExtensibility2_OnConnection
code) Store the current instance of Project passed to the Add-In from
the OnConnection code as "Application"

set gblAppInstance = Application
Set oAppEvents = New MyClass
Set oAppEvents.MyClass = gblAppInstance ' turn on events by tying the
add-in class to the current instance of Project.

now any time you change something in a Project document, the on-change event
fires and you can process the change (for example, I have a field that is in
units of hours, if the user enters "2.5d" the value displayed in the field
is 2.5*activeproject.hoursperday)

--
_________________________________________________________
Mark Durrenberger, PMP
Principal, Oak Associates, Inc, www.oakinc.com
"Advancing the Theory and Practice of Project Management"
________________________________________________________

The nicest thing about NOT planning is that failure
comes as a complete surprise and is not preceded by
a period of worry and depression.

- Sir John Harvey-Jones
 
L

Lars Hammarberg

Make the object public instead of private, and then, after you create your
instance of the class, you set the ProjEvents object to the current instance
of your project application. You have one ref to the app available in the
addin IDTExtensibility2_OnConnection event that you can use.
I.e.:
set myclass = new clsMyClass
set myclass.ProjEvents = AppInstance

--

/Lars Hammarberg
www.camako.se
MSProject Premier Partner
 

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