code for event: everytime i save an item ...

  • Thread starter Thread starter Patrik
  • Start date Start date
P

Patrik

outlook 2007:
i would like to start a VBA code everytime i click to save (new or changed
appointment? -> so do ...)
is this possible with vBA?
 
It's possible. You need to get a reference to the item that's opened and
then handle its Write() event. You get a NewInspector() event when an item
is opened, that would let you get a handle to the item being opened. You get
NewInspector() when you handle events for the Inspectors collection. So for
VBA this would work:

1. Select the ThisOutlookSession class module. Add your code there.

2. Add this code:

Private WithEvents colInsp As Outlook.Inspectors
Private 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
boolStartup = True
Set oAppt = Inspector.CurrentItem
End If
End Sub

Private Sub oAppt_Write(Cancel As Boolean)
' whatever
End Sub

That will initialize things when Outlook starts up and whenever you open an
appointment item you can then handle its Write() event.
 
where could/should i edit the code if i also want execute some code if the
item is deleted?
(not only new/changed)
 
In Outlook 2007 you can use the Folder.BeforeItemMove() event. That passes
you a reference to the item being moved and the target folder the item is
being moved to. If the target folder is Nothing then the item is being
deleted. There's also a Cancel argument, set that to True in the event
handler and that cancels the deletion.

So you'd declare the Folder object:

Private WithEvents oFolder As Outlook.Folder

That exposes the event handler for BeforeItemMove() in the Declarations
drop-down when oFolder is selected. You'd have to decide which folder to
use, for example if the Calendar folder then your initialization code would
have a line like this:

Set oFolder =
Application.GetNameSpace("MAPI").GetDefaultFolder(olFolderCalendar)
 
Back
Top