如何使用c#或vb从pst文件中获取outlook日历的名称?

时间:2015-04-08 11:13:50

标签: c# vb.net visual-studio outlook-addin

我想在Outlook中开发一个add来保存.ICS格式的日历。

我试过这段代码,但需要一个我应该买的课:/ !!

' Load the Outlook PST file
Dim pst As PersonalStorage = PersonalStorage.FromFile("d:\Data\Emails\PersonalStorage.pst")
' Get the Calendar folder
Dim folderInfo As FolderInfo = pst.RootFolder.GetSubFolder("Calendar")
' Loop through all the calendar items in this folder
Dim messageInfoCollection As MessageInfoCollection = folderInfo.GetContents()
For Each messageInfo As MessageInfo In messageInfoCollection
    ' Get the calendar information
    Dim calendar As MapiCalendar = CType(pst.ExtractMessage(messageInfo).ToMapiMessageItem(), MapiCalendar)
    ' Display some contents on screen
    Console.WriteLine("Name: " & calendar.Subject)
    ' Save to disk in ICS format
    calendar.Save("Calendar\" & calendar.Subject & ".ics", AppointmentSaveFormat.Ics)
Next messageInfo

1 个答案:

答案 0 :(得分:1)

Outlook中的日历由包含Outlook项目的文件夹表示。 Folder类提供GetCalendarExporter方法,该方法为指定的Folder创建CalendarSharing对象。请注意,GetCalendarExporter方法只能用于日历文件夹。如果您在表示其他文件夹类型的Folderobject上使用该方法,则会发生错误。

CalendarSharing类提供SaveAsICal方法,该方法将CalendarSharing对象的父文件夹中的日历信息导出为iCalendar日历(.ics)文件。

以下VBA示例为Calendar文件夹创建CalendarSharing对象,然后将整个文件夹(包括附件和私有项)的内容导出到iCalendar日历(.ics)文件。

Public Sub ExportEntireCalendar() 
 Dim oNamespace As NameSpace  
 Dim oFolder As Folder  
 Dim oCalendarSharing As CalendarSharing  
 On Error GoTo ErrRoutine 
 ' Get a reference to the Calendar default folder  
 Set oNamespace = Application.GetNamespace("MAPI")
 Set oFolder = oNamespace.GetDefaultFolder(olFolderCalendar) 
 ' Get a CalendarSharing object for the Calendar default folder. 
 Set oCalendarSharing = oFolder.GetCalendarExporter 
 ' Set the CalendarSharing object to export the contents of 
 ' the entire Calendar folder, including attachments and 
 ' private items, in full detail. 
 With oCalendarSharing 
 .CalendarDetail = olFullDetails 
 .IncludeWholeCalendar = True 
 .IncludeAttachments = True 
 .IncludePrivateDetails = True 
 .RestrictToWorkingHours = False 
 End With 
 ' Export calendar to an iCalendar calendar (.ics) file. 
 oCalendarSharing.SaveAsICal "C:\SampleCalendar.ics"  
 EndRoutine: 
 On Error GoTo 0  
 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