0

I'm new to outlook vba.

I'd like to run a macro when I create a new appointment in mycalendar in Outlook 2016 32bit

I tried with

Private WithEvents appt As Outlook.AppointmentItem

Private Sub appt_Write(Cancel As Boolean)
MsgBox ("test ok")
End Sub

in the ThisOutlookSession module but nothing happens when I edit anda save a new appointment.

What I have to do?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Massimo Griffani
  • 767
  • 8
  • 18

2 Answers2

2

You apt variable is never initialized and remains null. Try to use Application.Inspectors.MewInspector event to check when a new appointment is opened and set apt to Inspector.CurrentItem after checking that it is really an appointment. Note that you can have more than one appointment open, so a single apt variable won't work, you need to have a list or array.

Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78
2

From outlook event newMail (newItem)

Private WithEvents appt As AppointmentItem
Private WithEvents objinspectors As Outlook.Inspectors

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

Private Sub objinspectors_NewInspector(ByVal Inspector As Inspector)
    If TypeName(Inspector.currentItem) = "AppointmentItem" Then
        MsgBox "newinspector"
        Set appt = Inspector.currentItem    ' <----
    End If
End Sub

Private Sub appt_Write(Cancel As Boolean)
    MsgBox ("test ok")
End Sub

appt will be the most recently opened appointment item

Community
  • 1
  • 1
niton
  • 8,771
  • 21
  • 32
  • 52
  • There's a problem: the macro works just once. If I add a second appointment nothing happen unless i run Application_Startup again. WHY? – Massimo Griffani May 19 '17 at 07:52