在共享日历中创建Outlook约会

时间:2019-08-14 07:40:55

标签: excel vba outlook

我设置了以下代码,以便根据Excel电子表格中的数据在Outlook中创建约会。我想通过id进行约会,而不是使用我自己的默认日历。

我要添加到的日历是这里显示的DTS Streetworks日历-https://ibb.co/tKXKSPX,但我不知道如何处理。

Sub CoringAdd()

    Dim OL As Outlook.Application, ES As Worksheet, _
    r As Long, i As Long, wb As ThisWorkbook

    Set wb = ThisWorkbook
    Set ES = wb.Sheets("Coring")
    Set OL = New Outlook.Application

    r = ES.Cells(Rows.Count, 1).End(xlUp).Row
    For i = 2 To r
        With ES.Cells(i, 10)
            If .Value = "No" And ES.Cells(i, 7) <> "Yes" Then
                ES.Cells(i, 7) = "Yes"
                With OL.CreateItem(olAppointmentItem)
                    .Subject = "Send reminder email - LBRuT " + ES.Cells(i, 2).Value
                    .Start = ES.Cells(i, 6) + 1 + ES.Cells(i, 8).Value
                    .ReminderSet = True
                    .ReminderMinutesBeforeStart = 60
                    .Body = "£" & ES.Cells(i, 5).Value
                    .Save
                End With
            End If
        End With
    Next i

    Set OL = Nothing
    Set wb = Nothing
    Set ES = Nothing

End Sub

更新:

下面的最新代码,仍保留默认日历。

Sub ResolveName()

Dim OL As Outlook.Application, ES As Worksheet, _
    r As Long, i As Long, wb As ThisWorkbook

    Set wb = ThisWorkbook
    Set ES = wb.Sheets("Licences")
    Set OL = New Outlook.Application
    Dim myOlApp As Outlook.Application
    Dim myNamespace As Outlook.Namespace
    Dim myRecipient As Outlook.Recipient
    Dim CalendarFolder As Outlook.MAPIFolder
    Set myOlApp = CreateObject("Outlook.Application")
    Set myNamespace = myOlApp.GetNamespace("MAPI")
    Set myRecipient = myNamespace.CreateRecipient("DTS Streetworks")
    myRecipient.Resolve


    r = Cells(Rows.Count, 1).End(xlUp).Row
    For i = 5 To r
        With Cells(i, 5)

         If myRecipient.Resolved And .Value = "Mobile Plant" And Cells(i, 6) <> "" Then
    With OL.CreateItem(olAppointmentItem)
                    .Subject = "Test " + ES.Cells(i, 4).Value
                    .Start = ES.Cells(i, 14) + ES.Cells(i, 15).Value
                    .ReminderSet = True
                    .ReminderMinutesBeforeStart = 60
                    .Body = ES.Cells(i, 5).Value
                    .Save
    End With
    End If
    End With
    Next i
End Sub

Sub ShowCalendar(myNamespace, myRecipient)
    Dim CalendarFolder As Outlook.MAPIFolder
    Set CalendarFolder = myNamespace.GetSharedDefaultFolder(myRecipient, olFolderCalendar)
    CalendarFolder.Display
End Sub

1 个答案:

答案 0 :(得分:0)

您可以使用NameSpace.GetSharedDefaultFolder方法获取共享日历,该方法返回一个Folder对象,该对象代表指定用户的指定默认文件夹。例如:

Sub ResolveName()
    Dim myOlApp As Outlook.Application
    Dim myNamespace As Outlook.NameSpace
    Dim myRecipient As Outlook.Recipient
    Dim CalendarFolder As Outlook.MAPIFolder
    Set myOlApp = CreateObject("Outlook.Application")
    Set myNamespace = myOlApp.GetNamespace("MAPI")
    Set myRecipient = myNamespace.CreateRecipient("Eugene Astafiev")
    myRecipient.Resolve
    If myRecipient.Resolved Then
        Call ShowCalendar(myNamespace, myRecipient)
    End If
End Sub

Sub ShowCalendar(myNamespace, myRecipient)
    Dim CalendarFolder As Outlook.MAPIFolder
    Set CalendarFolder = myNamespace.GetSharedDefaultFolder(myRecipient, olFolderCalendar)
    CalendarFolder.Display
End Sub

获得共享日历文件夹后,可以使用Items.Add方法,该方法在Items集合中为该文件夹创建一个新的Outlook项目。您只需要传递需要创建的项目类型,例如olAppointmentItem

Set myItem = mySharedCalendarFolder.Items.Add olAppointmentItem

因此,您的代码应如下所示:

    Set wb = ThisWorkbook
    Set ES = wb.Sheets("Licences")

    Dim myOlApp As Outlook.Application
    Dim myNamespace As Outlook.Namespace
    Dim myRecipient As Outlook.Recipient
    Dim CalendarFolder As Outlook.MAPIFolder
    Dim olAppItem as Outlook.AppointmentItem
    Set myOlApp = CreateObject("Outlook.Application")
    Set myNamespace = myOlApp.GetNamespace("MAPI")
    Set myRecipient = myNamespace.CreateRecipient("DTS Streetworks")
    myRecipient.Resolve

    If myRecipient.Resolved Then
        Set CalendarFolder = myNamespace.GetSharedDefaultFolder(myRecipient, olFolderCalendar)
        r = Cells(Rows.Count, 1).End(xlUp).Row
        For i = 5 To r
           With Cells(i, 5)
             If .Value = "Mobile Plant" And Cells(i, 6) <> "" Then
                Set olAppItem = CalendarFolder.Items.Add olAppointmentItem
                With olAppItem 
                    .Subject = "Test " + ES.Cells(i, 4).Value
                    .Start = ES.Cells(i, 14) + ES.Cells(i, 15).Value
                    .ReminderSet = True
                    .ReminderMinutesBeforeStart = 60
                    .Body = ES.Cells(i, 5).Value
                    .Save
                End With
             End If
         End With
       Next i

End Sub