Make use of Events in Outlook

J

Jan T.

Hi. I want to run a procedure Sub MyProc() when I save or close an
appointment
that I just have created. I know there are some Events that are triggered,
but how
can I write code to make use of those Events?

Class AppointmentItem
Event Close(Cancel As Boolean)
Event CustomAction(Action As Object, Response As Object, Cancel As Boolean)

I appreciate very much any help, examples or suggestions. Thank's in
advance!

Regards
Jan T.
 
K

Ken Slovak

The easiest way to handle events in Outlook VBA code is to use the
ThisOutlookSession class module, which is always in scope.

To get events on an appointment you need to handle first NewInspector() to
instantiate an event handling item and then handle events on the item.

In ThisOutlookSession, at the class level:

Dim WithEvents colInsp As Outlook.Inspectors
Dim WithEvents oAppt As Outlook.AppointmentItem

Private Sub Application_Startup()
Set colInsp = Application.Inspectors
End Sub

Private Sub colInspectors_NewInspector(Inspector As Inspector)
If Inspector.CurrentItem.Class = olAppointment Then
Set oAppt = Inspector.CurrentItem
End If
End Sub

That will instantiate the appointment object as an event handling object and
you then can handle the Close() and Write() events on the item.
 
J

Jan T.

Hi again and thank you for your answer. However I am having some problems to
put in
what ever is missing. I tried to implement the code as below but not very
successfully:

Dim WithEvents colInsp As Outlook.Inspectors
Dim WithEvents oAppt As Outlook.AppointmentItem

Private Sub Application_Startup()
Set colInsp = Application.Inspectors
End Sub

Private Sub colInspectors_NewInspector(Inspector As Inspector)
If Inspector.CurrentItem.Class = olAppointment Then
Set oAppt = Inspector.CurrentItem
MsgBox "New Inspector.."
End If
End Sub

Private Sub oAppt_Open(Cancel As Boolean)
MsgBox "New appointment item.."
End Sub

All code above, I put into ThisOutlookSession.
What am I missing here?
 
K

Ken Slovak

What isn't working? You don't mention that. Are you getting the
NewInspector() event firing? What about the other events? Does the startup
code run? Can you run it manually?

It's pretty tough to guess remotely with no clues. You need to be specific
as to what's going on.
 
J

Jan T.

The Private Sub Application_Startup() fires, but nothing happens when I
I open a new mail?
Neither the Private Sub colInspectors_NewInspector(Inspector As Inspector)
nor the Private Sub oAppt_Open(Cancel As Boolean) fires?

Regards
Jan T.
 
K

Ken Slovak

That's odd.

If you put your cursor in the startup code and click F5 does it run? Put a
breakpoint in the startup code and see if you hit it. Do the same in
NewInspector() and see if it gets hit when you open any item. Put the
NewInspector() breakpoint on the Sub line.

Outlook version? What are your macro settings?
 
J

Jan T.

I am using Outlook 2000 and Security is set to Medium.
Activate Macros is accepted by me when I open Outlook.
I have been checkin with both breakpoints in the Startup code
and msgboxes to let me know when an Event is trigged.

Everything is in ThisOutlookSession, right?

Jan T.
 
J

Jan T.

Sorry, I forgot to tell you that it the startUp code fires, but nothing
else. Jan T.
 
J

Jan T.

I found out what was wrong I think. First, the code
Set colInsp = Application.Inspectors did not match with
the name in InspectorEvent procedure where it says:
Private Sub colInspectors_NewInspector(Inspector As Inspector)

"colInsp" and "colInspectors" does not refer to each other.

Second, I found that the keyword "ByVal" was missing in (ByVal Inspector as
Inspector)

After changing that, the code worked fine and now looks like the following:

Dim WithEvents colInsp As Outlook.Inspectors
Dim WithEvents oAppt As Outlook.AppointmentItem

Private Sub Application_Startup()
Set colInsp = Application.Inspectors
End Sub

Private Sub colInsp_NewInspector(ByVal Inspector As Inspector)
If Inspector.CurrentItem.Class = olAppointment Then
Set oAppt = Inspector.CurrentItem
MsgBox "testing code.."
End If
End Sub

Private Sub oAppt_Open(Cancel As Boolean)
MsgBox "Opening a new Appointment"
End Sub

Both messageboxes do now show when a new appointment is opened.

Thanx anyway for all code and help! appreciated that.

Regards
Jan

**************************************************************
 
J

Jan T.

OK, I finally found out what was wrong I think. First, the code
Set colInsp = Application.Inspectors did not match with
the name in InspectorEvent procedure where it says:
Private Sub colInspectors_NewInspector(Inspector As Inspector)

"colInsp" and "colInspectors" does not refer to each other.

Second, I found that the keyword "ByVal" was missing in (ByVal Inspector as
Inspector)

After changing that, the code worked fine and now looks like the following:

Dim WithEvents colInsp As Outlook.Inspectors
Dim WithEvents oAppt As Outlook.AppointmentItem

Private Sub Application_Startup()
Set colInsp = Application.Inspectors
End Sub

Private Sub colInsp_NewInspector(ByVal Inspector As Inspector)
If Inspector.CurrentItem.Class = olAppointment Then
Set oAppt = Inspector.CurrentItem
MsgBox "testing code.."
End If
End Sub

Private Sub oAppt_Open(Cancel As Boolean)
MsgBox "Opening a new Appointment"
End Sub

Both messageboxes do now show when a new appointment is opened.

Thanx anyway for all code and help! appreciated that.

Regards
Jan

**************************************************************
 

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