將約會 XML 資料匯入 Outlook 約會物件 (Outlook)

本主題顯示如何讀取 XML 格式的約會資料,將資料儲存到預設行事曆中的 Microsoft Outlook AppointmentItem 物件,並以陣列傳回約會物件。

| MVP 標誌

|Helmut Obertanner 提供下列程式碼範例。 Helmut 是 Microsoft 最有價值專家 ,具備 Microsoft Visual Studio 和 Microsoft Office Outlook 中 Microsoft Office 開發工具的專業知識。|

The following managed code samples are written in C# and Visual Basic. To run a .NET Framework managed code sample that needs to call into a Component Object Model (COM), you must use an interop assembly that defines and maps managed interfaces to the COM objects in the object model type library. 針對 Outlook,請使用 Visual Studio 和 Outlook 主要 Interop 元件 (PIA) 。 在執行 Outlook 2013 的 Managed 程式碼範例之前,請確定您已安裝 Outlook 2013 PIA,並已在 Visual Studio 中新增 Microsoft Outlook 15.0 物件程式庫元件的參考。 使用 Office Developer Tools for Visual Studio) ,在 Outlook 增益集 (類別中使用下列程式碼 ThisAddIn 範例。 程式碼中的Application物件必須是 所 ThisAddIn.Globals 提供的受信任 Outlook應用程式物件。 如需使用 Outlook PIA 開發受控 Outlook 解決方案的詳細資訊,請參 閱歡迎使用 Outlook 主要 Interop 元件參考。 下列程式碼範例包含 CreateAppointmentsFromXml 類別的 Sample 方法,實作為 Outlook 增益集專案的一部分。 每個專案都會新增 Outlook PIA 的參考,以 Microsoft.Office.Interop.Outlook 命名空間為基礎。 方法 CreateAppointmentsFromXml 接受兩個輸入參數: 應用程式xml

  • 應用程式 是受信任的 Outlook 應用程式 物件。

  • xml 是一個 XML 字串,或是代表有效之 XML 檔的路徑。 基於下列程式碼範例的目的,XML 使用下列 XML 標籤來分隔約會資料:

約會資料 分隔 XML 標籤
整組約會資料 <約會>
該組的每一項約會 <委任>
約會的開始時間 <starttime>
約會的結束時間 <endtime>
約會的標題 <主題>
約會的位置 <位置>
約會的詳細資料 <身體>

下列範例會顯示 xml 參數的輸入資料。

<?xml version="1.0" encoding="utf-8" ?>  
<appointments> 
    <appointment> 
        <starttime>2009-06-01T15:00:00</starttime> 
        <endtime>2009-06-01T16:15:00</endtime> 
        <subject>This is a Test-Appointment</subject> 
        <location>At your Desk</location> 
        <body>Here is the Bodytext</body> 
    </appointment> 
    <appointment> 
        <starttime>2009-06-01T17:00:00</starttime> 
        <endtime>2009-06-01T17:15:00</endtime> 
        <subject>This is a second Test-Appointment</subject> 
        <location>At your Desk</location> 
        <body>Here is the Bodytext</body> 
    </appointment> 
    <appointment> 
        <starttime>2009-06-01T17:00:00</starttime> 
        <endtime>2009-06-01T18:15:00</endtime> 
        <subject>This is a third Test-Appointment</subject> 
        <location>At your Desk</location> 
        <body>Here is the Bodytext</body> 
    </appointment> 
</appointments> 

方法 CreateAppointmentsFromXml 會使用 XML 檔物件模型的 Microsoft COM 實作 (DOM) 來載入和處理 xml 提供的 XML 資料。 CreateAppointmentsFromXml 首先檢查 xml 是否指定有效的 XML 資料來源。 如果是,它會將資料載入 XML 文件 DOMDocument。 否則,會 CreateAppointmentsFromXml 擲回例外狀況。 如需 XML DOM 的詳細資訊,請參閱 DOM。 針對以 XML 資料中的約會 > 標籤分隔 < 的每個約會子節點, CreateAppointmentsFromXml 尋找特定標記、使用 DOM 擷取資料,並將資料指派給AppointmentItem物件的對應屬性:StartEndSubjectLocationBodyCreateAppointmentsFromXml 然後將約會儲存到預設行事曆。 CreateAppointmentsFromXml會在System.Collections.Generic命名空間中使用 List (類型) 類別的Add方法來匯總這些AppointmentItem物件。 當此方法已處理 XML 資料中的所有約會之後,它會以陣列傳回 AppointmentItem 物件。 下列為 C# 程式碼範例。

using System; 
using System.Collections.Generic; 
using System.IO; 
using System.Text; 
using System.Xml; 
using Outlook = Microsoft.Office.Interop.Outlook; 
 
namespace OutlookAddIn1 
{ 
    class Sample 
    { 
        Outlook.AppointmentItem[] CreateAppointmentsFromXml(Outlook.Application application, 
                                                            string xml) 
        { 
            // Create a list of appointment objects. 
            List<Outlook.AppointmentItem> appointments = new  
                List<Microsoft.Office.Interop.Outlook.AppointmentItem>(); 
            XmlDocument xmlDoc = new XmlDocument(); 
 
            // If xml is an XML string, create the document directly.  
            if (xml.StartsWith("<?xml")) 
            { 
                xmlDoc.LoadXml(xml); 
            } 
            else if (File.Exists(xml)) 
            { 
                xmlDoc.Load(xml); 
            } 
            else 
            { 
                throw new Exception( 
                    "The input string is not valid XML data or the specified file doesn't exist."); 
            } 
 
            // Select all appointment nodes under the root appointments node. 
            XmlNodeList appointmentNodes = xmlDoc.SelectNodes("appointments/appointment"); 
            foreach (XmlNode appointmentNode in appointmentNodes) 
            { 
 
                // Create a new AppointmentItem object. 
                Outlook.AppointmentItem newAppointment =  
                    (Outlook.AppointmentItem)application.CreateItem(Outlook.OlItemType.olAppointmentItem); 
 
                // Loop over all child nodes, check the node name, and import the data into the  
                // appointment fields. 
                foreach (XmlNode node in appointmentNode.ChildNodes) 
                { 
                    switch (node.Name) 
                    { 
 
                        case "starttime": 
                            newAppointment.Start = DateTime.Parse(node.InnerText); 
                            break; 
 
                        case "endtime": 
                            newAppointment.End = DateTime.Parse(node.InnerText); 
                            break; 
 
                        case "subject": 
                            newAppointment.Subject = node.InnerText; 
                            break; 
 
                        case "location": 
                            newAppointment.Location = node.InnerText; 
                            break; 
 
                        case "body": 
                            newAppointment.Body = node.InnerText; 
                            break; 
 
                    } 
                } 
 
                // Save the item in the default calendar. 
                newAppointment.Save(); 
                appointments.Add(newAppointment); 
            } 
 
            // Return an array of new appointments. 
            return appointments.ToArray(); 
        } 
 
    } 
}

下列為 Visual Basic 程式碼範例。

Imports System.IO 
Imports System.Xml 
Imports Outlook = Microsoft.Office.Interop.Outlook 
 
Namespace OutlookAddIn2 
    Class Sample 
        Function CreateAppointmentsFromXml(ByVal application As Outlook.Application, _ 
            ByVal xml As String) As Outlook.AppointmentItem() 
 
            Dim appointments As New List(Of Outlook.AppointmentItem) 
            Dim xmlDoc As New XmlDocument() 
 
            If xml is an XML string, create the XML document directly. 
            If xml.StartsWith("<?xml") Then 
                xmlDoc.LoadXml(xml) 
            ElseIf (File.Exists(xml)) Then 
                xmlDoc.Load(xml) 
            Else 
                Throw New Exception("The input string is not valid XML data or the specified file doesn't exist.") 
            End If 
 
 
            ' Select all appointment nodes under the root appointments node. 
            Dim appointmentNodes As XmlNodeList = xmlDoc.SelectNodes("appointments/appointment") 
 
            For Each appointmentNode As XmlNode In appointmentNodes 
 
                ' Create a new AppointmentItem object. 
                Dim newAppointment As Outlook.AppointmentItem = _ 
                    DirectCast(application.CreateItem(Outlook.OlItemType.olAppointmentItem), _ 
                    Outlook.AppointmentItem) 
 
                ' Loop over all child nodes, check the node name, and import the data into the appointment fields. 
 
                For Each node As XmlNode In appointmentNode.ChildNodes 
                    Select Case (node.Name) 
 
                        Case "starttime" 
                            newAppointment.Start = DateTime.Parse(node.InnerText) 
 
 
                        Case "endtime" 
                            newAppointment.End = DateTime.Parse(node.InnerText) 
 
 
                        Case "subject" 
                            newAppointment.Subject = node.InnerText 
 
 
                        Case "location" 
                            newAppointment.Location = node.InnerText 
 
 
                        Case "body" 
                            newAppointment.Body = node.InnerText 
 
 
                    End Select 
                Next 
 
                ' Save the item in the default calendar. 
                newAppointment.Save() 
                appointments.Add(newAppointment) 
            Next 
 
            ' Return an array of new appointments. 
            Return appointments.ToArray() 
        End Function 
 
 
    End Class 
End Namespace

支援和意見反應

有關於 Office VBA 或這份文件的問題或意見反應嗎? 如需取得支援服務並提供意見反應的相關指導,請參閱 Office VBA 支援與意見反應