Events Question

K

kalukaley

Below is a copy of the code that I'm using to catch when someone updates an
item in their calendar.

How would I alter this code to capture an event any appointment in the
calendar.
Also, if one of the appointments is a recurring appointment how do I capture
the update of a single item in the series rather than an update to the entire
series?

Public WithEvents myItem As AppointmentItem

Private Sub Application_Startup()
Set myItem =
Application.GetNamespace("MAPI").GetDefaultFolder(olFolderCalendar).Items.GetFirst

End Sub

Private Sub myItem_PropertyChange(ByVal Name As String)
MsgBox "The " & Name & " property changed."
End Sub
 
K

Ken Slovak - [MVP - Outlook]

That event you're handling won't catch a lot of changes. It also will fail
to work after a while since the myItem object will be out of scope once
Application_Startup() finishes and the garbage collector will remove that
reference and its events. Always declare an object variable at a class level
to keep it alive and therefore keep the events alive.

You can handle the events for the Items collection of the Calendar folder.
Declare that Items collection at class level and then handle the ItemAdd,
ItemChange and ItemRemove events.

For recurring events you have to get the master appointment, after testing
IsRecurring on the item. If it is recurring then use GetRecurrencePattern to
get at RecurrencePattern.Parent to get the master appointment. Never access
the RecurrencePattern unless IsRecurring is true, it will make the
non-recurring item recurring.

Once you have the master appointment you can see if there are any changes to
individual occurrences by checking the RecurrencePattern.Exceptions
collection, which has all modified and deleted occurrences.
 
K

kalukaley

I'm still waiting for my Outlook Programming book to arrive so, I'm relying
on online samples.

HANDLE ITEMS COLLECTION AT CLASS LEVEL
-----------------------------------------------------
I took this to mean that I should use the class Module

i've looked at many of the MSDN examples and they say to declare my
"Initialize_handler" function in a class module along with the handlers.

They say to call Initialize_handler method before events can be handled. I
tried calling it from "Application_Startup()" in "ThisOutlookSession" but I
just get the message Sub or Fucntion not defined.

-------------------------------------------------------------------------------
Code from class Module
Note: I really only threw in a msgbox to make sure the method
was being called. I'm not going to bother writing extra code, if I can't
make sure
the method is being called
-------------------------------------------------------------------------------
Public WithEvents CalItems As Outlook.items

Public Sub Initialize_handler()

Set CalItems =
Application.GetNamespace("MAPI").GetDefaultFolder(olFolderCalendar).items

MsgBox "I'm here"
End Sub

-------------------------------------------------------------
Code from "ThisOutlookSession"
-------------------------------------------------------------

Private Sub Application_Startup()
Initialize_handler

End Sub
 
K

Ken Slovak - [MVP - Outlook]

If the event handler and init code is in a class module you'd have to
instantiate an instance of that class in the Application_Startup() code, and
the class would have to retain scope after Application_Startup() ended.

A class has no existence until an instance of it is instantiated.

Let's say the class module was named myClass. Then to use it you'd do this
in ThisOutlookSession:

Dim classHandler As New myClass

Then in Application_Startup() the call would look like this:

classHandler.Initialize_handler

A simpler approach would be to move all of your class code into the
ThisOutlookSession class module. The WithEvents declarations would be at the
top of the module, above any procedures. Initialize_handler() would be in
ThisOutlookSession also and could be Private if you want, it wouldn't have
to be Public.

In that case the call to the Initialize_handler would look like it does now.
 
K

kalukaley

Almost There. I noticed that I didn't include the actual hanlder. The
Inialize_handler is now being called (message box is working).

However, the ItemChange Event doesn't appear to be called.

I have a recurring Appointment in my calendar. as a simple test I change
the duration or start time ( or any other thing ) in the appointment then
save it.
At this point I'm expecting a pop-up from my eventhandler ... but nothing
happens.

P.S. The future of this code is more than just dopey pop-ups :)


--------------------------------------------------------------------
From CLASS MODULE
-------------------------------------------------------------------
Public WithEvents CalItems As Outlook.items

Public Sub Initialize_handler()

Set CalItems =
Application.GetNamespace("MAPI").GetDefaultFolder(olFolderCalendar).items

MsgBox "I'm here"
End Sub


Public Sub CalItems_ItemChange(ByVal Item As Object)
MsgBox "now I am here"

End Sub

-----------------------------------------------------------------------
From ThisOutLookSession
-----------------------------------------------------------------------
Dim classHandler As New Class1


Private Sub Application_Startup()

classHandler.Initialize_handler



End Sub
 
K

Ken Slovak - [MVP - Outlook]

Does the event fire if you modify a non-recurring appointment?
 
K

kalukaley

Thanks Ken, turns out that I left out a simple step. I needed to close out
of outlook and get back in; all the tidbits you gave me were right on target.

Do you have a website. I'd like to go through your information.

There's a lot of information out there, but a lot of it assumes a certain
level of knowledge that tyro's like me simply don't have.

Thanks again.
 
K

Ken Slovak - [MVP - Outlook]

My Web site is referenced in my signature.

The best reference for Outlook coding is at www.outlookcode.com. That site
has tons of sample code and articles about developing for Outlook.
 

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