run a macro when creating new appointment in outlook

邮差的信 提交于 2019-12-12 04:08:17

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!