共用方式為


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

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

| MVP 標誌

|Helmut Obertanner 提供了以下程式碼範例。 Helmut 是 Microsoft 最有價值專業人士 ,精通 Microsoft Office 開發工具、Microsoft Visual Studio 及 Microsoft Office Outlook。

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 主要的互操作組件 (PIA) 。 在執行 Outlook 2013 的受管程式碼範例前,請確保你已安裝 Outlook 2013 PIA,並在 Visual Studio 中新增 Microsoft Outlook 15.0 物件函式庫元件的參考。 請使用以下 Outlook 外掛類別的程式碼範例 ThisAddIn , (使用 Office 開發者工具 for Visual Studio) 。 程式碼中的應用程式物件必須是由 ThisAddIn.Globals提供 的受信任 Outlook 應用程式物件。 欲了解更多使用 Outlook PIA 來開發受管理 Outlook 解決方案的資訊,請參閱 「歡迎來到 Outlook 主要互操作組合語言參考」。 以下程式碼範例包含 CreateAppointmentsFromXml 了該 Sample 類別的方法,作為 Outlook 外掛專案的一部分實作。 每個專案都會新增一個指向 Outlook PIA 的參考,該 PIA 基於 Microsoft.Office.Interop.Outlook 命名空間。 此 CreateAppointmentsFromXml 方法接受兩個輸入參數, 應用程式xml

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

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

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

下列範例會顯示 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 方法使用Microsoft COM 實作的 XML 文件物件模型 (DOM) 來載入並處理 XML 所提供的 XML 資料。 CreateAppointmentsFromXml 首先檢查 XML 是否指定有效的 XML 資料來源。 如果是,它會將資料載入 XML 文件 DOMDocument。 否則, CreateAppointmentsFromXml 會拋出例外。 欲了解更多關於 XML DOM 的資訊,請參見 DOM。 對於 XML 資料中以 <appointment> 標籤劃分的每個 appointment 子節點, 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 支援與意見反應