使用有效负载共享导出日历Export a Calendar using Payload Sharing

Microsoft Outlook 包括使用附加到**MailItem** 的 iCalendar (ics) 文件与其他用户共享日历信息的功能。Microsoft Outlook includes the ability to share calendar information with other users by using an iCalendar (.ics) file attached to a MailItem. CalendarSharing 对象用于从包含日历项目的文件夹中生成 icalendar 文件, 并生成 icalendar 文件附加到的MailItemThe CalendarSharing object is used to both generate the iCalendar file from a folder containing calendar items and to generate the MailItem to which the iCalendar file is attached.

本示例将使用 CalendarSharing 项目与一个收件人共享接下来七天的空闲/忙碌信息:This sample uses the CalendarSharing item to share free/busy information for the next seven days with a single recipient:

  1. 本示例使用 NameSpace 对象的 GetDefaultFolder 方法来获取对当前用户的 "日历" 默认文件夹的 Folder 对象引用。The sample obtains a Folder object reference for the Calendar default folder for the current user, by using the GetDefaultFolder method of the NameSpace object.

  2. 它使用folder对象的**GetCalendarExporter** 方法来创建文件夹的CalendarSharing对象引用。It uses the GetCalendarExporter method of the Folder object to create a CalendarSharing object reference for the folder.

  3. 接下来,本示例将对 CalendarSharing 对象设置下列属性,以限制该对象导出信息的范围和级别:It then sets the following properties on the CalendarSharing object to restrict the range and level of information exported by the object:

    • 设置 CalendarDetail 属性,以便将每个日历项目的信息仅限制为空闲/忙碌信息。The CalendarDetail property is set to limit the information for each calendar item to free/busy information only.

    • 设置 StartDateEndDate 属性,将文件中包括的日历项目的期限限制为接下来七天。The StartDate and EndDate properties are set to restrict the calendar items included in the file to the next seven days.

    • 设置 RestrictToWorkingHours 属性,将日历项目限制为工作小时之内的项目。The RestrictToWorkingHours property is set to restrict the calendar items to those that fall within working hours.

    • 设置 IncludeAttachments 属性,排除该对象导出的日历项目的任何附件。The IncludeAttachments property is set to exclude any attachments for calendar items exported by the object.

    • 设置 IncludePrivateDetails 属性,排除该对象导出的任何个人日历项目的详细信息。The IncludePrivateDetails property is set to exclude the details of any private calendar items exported by the object.

  4. 然后, 它 calles CalendarSharing对象的**ForwardAsICal** 方法, 以将日历项目导出到 iCalendar 文件, 并创建带有 iCalendar 文件的MailItem对象作为附件。It then calles the ForwardAsICal method of the CalendarSharing object to export the calendar items to an iCalendar file and create a MailItem object with the iCalendar file as an attachment. olCalendarMailFormat 枚举的olCalendarMailFormatDailySchedule常量与ForwardAsICal方法一起使用, 以指示MailItem的正文应包含 HTML 格式的忙/闲信息未来七天的信息。The olCalendarMailFormatDailySchedule constant of the olCalendarMailFormat enumeration is used with the ForwardAsICal method to indicate that the body of the MailItem should contain, in HTML format, free/busy information for the next seven days.

  5. 最后, 将调用新创建的MailItem对象的**收件人** 集合的**add** 方法, 以添加指定的收件人, 并使用**send** 方法发送MailItemFinally, the Add method for the Recipients collection of the newly created MailItem object is called to add the specified recipient and the Send method is used to send the MailItem.

Public Sub ShareWorkCalendarByPayload() 
 
 Dim oNamespace As NameSpace 
 Dim oFolder As Folder 
 Dim oCalendarSharing As CalendarSharing 
 Dim oMailItem As MailItem 
 
 On Error GoTo ErrRoutine 
 ' Get a reference to the Calendar default folder 
 Set oNamespace = Application.GetNamespace("MAPI") 
 Set oFolder = oNamespace.GetDefaultFolder(olFolderCalendar) 
 
 ' Get a reference to a CalendarSharing object for that 
 ' folder. 
 Set oCalendarSharing = oFolder.GetCalendarExporter 
 
 ' Set the CalendarSharing object to restrict 
 ' the information shared in the iCalendar file. 
 With oCalendarSharing 
 ' Send free/busy information only. 
 .CalendarDetail = olFreeBusyOnly 
 
 ' Send information for the next seven days. 
 .startDate = Now 
 .endDate = DateAdd("d", 7, Now) 
 
 ' Restrict information to working hours only. 
 .RestrictToWorkingHours = True 
 
 ' Exclude attachments and private information. 
 .IncludeAttachments = False 
 .IncludePrivateDetails = False 
 End With 
 
 ' Get the mail item containing the iCalendar file 
 ' and calendar information. 
 Set oMailItem = oCalendarSharing.ForwardAsICal( _ 
 olCalendarMailFormatDailySchedule) 
 
 ' Send the mail item to the specified recipient. 
 With oMailItem 
 .Recipients.Add "someone@example.com" 
 .Send 
 End With 
 
EndRoutine: 
 On Error GoTo 0 
 Set oMailItem = Nothing 
 Set oCalendarSharing = Nothing 
 Set oFolder = Nothing 
 Set oNamespace = Nothing 
Exit Sub 
 
ErrRoutine: 
 Select Case Err.Number 
 Case 287 ' &H0000011F 
 ' The user denied access to the Address Book. 
 ' This error occurs if the code is run by an 
 ' untrusted application, and the user chose not to 
 ' allow access. 
 MsgBox "Access to Outlook was denied by the user.", _ 
 vbOKOnly, _ 
 Err.Number & " - " & Err.Source 
 Case -2147467259 ' &H80004005 
 ' Export failed. 
 ' This error typically occurs if the CalendarSharing 
 ' method cannot export the calendar information because 
 ' of conflicting property settings. 
 MsgBox Err.Description, _ 
 vbOKOnly, _ 
 Err.Number & " - " & Err.Source 
 Case -2147221233 ' &H8004010F 
 ' Operation failed. 
 ' This error typically occurs if the GetCalendarExporter method 
 ' is called on a folder that doesn't contain calendar items. 
 MsgBox Err.Description, _ 
 vbOKOnly, _ 
 Err.Number & " - " & Err.Source 
 Case Else 
 ' Any other error that may occur. 
 MsgBox Err.Description, _ 
 vbOKOnly, _ 
 Err.Number & " - " & Err.Source 
 End Select 
 
 GoTo EndRoutine 
End Sub

支持和反馈Support and feedback

有关于 Office VBA 或本文档的疑问或反馈?Have questions or feedback about Office VBA or this documentation? 请参阅 Office VBA 支持和反馈,获取有关如何接收支持和提供反馈的指南。Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.