Document Level Event Trapping (COM Add-in)

M

Mark Durrenberger

Has anyone been successful at trapping the On_open (or any document level
event in a com add-in. I'm having a heck of a time getting this to work.
Specifically I'm working with MS Project, but I'm hoping the setup is
similar to other Office products.

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 doc 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



--
_________________________________________________________
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
 
T

Tom Winter

My guess is that you are not setting your "ProjEvents" variable to an actual
Project object. It's setting the variable to an actual project that starts
generating the events. There is no quick/easy way to get these events for
all projects. If that's what you are looking for, you'll need to create a
"wrapper" class, like the one you already have, but create a new instance of
it for each Project that's opened/created. I hope that makes some sense.
Sorry, I have no experience in Project itself.
 
M

Mark Durrenberger

My guess is that you are not setting your "ProjEvents" variable to an
actual
Project object.
You are correct here. I might be able to do this later (say in the
StartupComplete code - would that make any sense?)

It's setting the variable to an actual project that starts
generating the events.
So when do I do this? It would seem to make sense to do this in the OPEN
event which I can't trap (is this a catch 22 :)

There is no quick/easy way to get these events for
all projects. If that's what you are looking for, you'll need to create a
"wrapper" class, like the one you already have, but create a new instance of
it for each Project that's opened/created.

I've heard of Wrappers but don't know what they are. Any quick hints?

I hope that makes some sense.
Sorry, I have no experience in Project itself.


Thanks for you thoughts, it points me in another research direction.

Mark
 
T

Tom Winter

A quick thought. I believe the only way you could get the Project Open event
is if within VBA code saved as part of the Project itself. From a COM Add-In
or other program, you won't be able to do that.

Also, from a quick look at the MDSN documentation, It looks like the
Application object has Project related events form Save, Print, Close,
Calculate. Will those work for you? It doesn't have Activate or Deactive,
though it does have those for Windows. Also the NewProject event may work
for Open. Again, I don't know anything about Project, so I'm just guessing

For wrappers, assume you have created a wrapper class for the Project, like
in your first post, with the WithEvents ProjEvents variable. Then in your
code the Application events, have something like this:

Dim oWrappers as Collection

Sub AppEvents_NewProject(oProject as MSProject.Project)

Dim oWrapper as ProjectWrapper

Set oWrapper.ProjEvents = oProject

oCollection.Add oWrapper

End Sub

This catches each new project and creates a Wrappe for it and hooks it up to
the project. Then each wrapper will get the events for that project.
 
M

Mark Durrenberger

I'm using the NEW PROJECT event now and may be stuck with it...

The issue is that when someone launches project, then closes open projects
and then tries to open a new (blankk) project - a number of my routines fail
because there is no active document

NEWPROJECT fires before an active document has been created.

The behavior is different when opening Project from a doubleclick on it's
icon. Assuming you have "open previous project" turned off, Project opens a
blank document and NEWPROJECT runs fine.

WHen open previous project is turned on, no problems at all

Mark


--
_________________________________________________________
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
 
M

Mark Durrenberger

Tom, for the time being, I've hacked around the issue... IF you open a file
from Project, my code tests to see if there is an actual docment object. If
there is none, it gives the user a message to manually run my setup code
(which is a feature I built long ago)...

It's not pretty, but it avoids the error messages ...

Thanks for your time.

FYI I've posted this question on about four news groups today and two VB
forums over the weekend. Yours was the only response... Kudos to you for
trying.

Mark

--
_________________________________________________________
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
 

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