保存联系人后创建约会

时间:2019-03-13 01:25:05

标签: vba outlook outlook-vba

创建联系人后,我正在编写宏以创建约会。

用户可以单击按钮,输入联系人的详细信息,并保存后应打开一个新的约会窗口。

问题是在保存联系人之前调用了约会子。因此,不存在要传递给子对象的对象/联系人。

我的代码:

' Application-level variables 
Public objOL As Outlook.Application 
Public objNS As Outlook.NameSpace

' Public Folders 
Public objPublicFolderRoot As Outlook.Folder 
Public objCompanyFolder As Outlook.Folder 
Public objContactFolder As Outlook.Folder 
Public objCalFolder As Outlook.Folder

' Set the Public Folders 
Set objPublicFolderRoot = objNS.GetDefaultFolder(olPublicFoldersAllPublicFolders) Set objCompanyFolder = objPublicFolderRoot.Folders("Company_Shared") Set objContactFolder = objCompanyFolder.Folders("Contacts") Set objCalFolder = objCompanyFolder.Folders("Calendars")

Sub CreateEmptyContact()
    Dim objContact As Outlook.ContactItem
    Set objNS = Application.GetNamespace("MAPI")

    ' Build out the contact item
    Set objContact = objContactFolder.Items.Add(olContactItem)

    With objContact
        .Display
    End With

    CreateAppointment(objContact)

    ' Reset vars
    Set objContact = Nothing
    Set objPublicFolderRoot = Nothing
    Set objCompanyFolder = Nothing
    Set objContactFolder = Nothing

End Sub

Public Sub CreateAppointment(objContact As Outlook.ContactItem)   
' Create the appointment on the public folder calendar with the passed contact Public Sub CreateAppointment(objContact As Outlook.ContactItem)
    Dim objCalAppt As Outlook.AppointmentItem
    Set objCalAppt = Application.CreateItem(olAppointmentItem)  

    ' Add to the Appointment to the existing Calendar items
    Set objCalAppt = objCalFolder.Items.Add(olAppointmentItem)
    With objCalAppt
        .MeetingStatus = olNonMeeting 'Not an invitation
        .Start = Now
        .Duration = 120
        .Save
        .Display
    End With

    ' Reset vars
    Set objCalAppt = Nothing   
End Sub

我前进的最佳方法是什么?例如,我应该在Contact_Send或Contact_AfterWrite上使用诸如WithEvents之类的东西吗?

1 个答案:

答案 0 :(得分:1)

您可以模态显示联系人(.Display(true),也可以观看Contacts文件夹中的Items.ItemAdd事件触发(保存联系人后)并使用传递给事件的项目处理程序。