问题
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?
回答1:
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.
回答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
来源:https://stackoverflow.com/questions/44027860/run-a-macro-when-creating-new-appointment-in-outlook