予定 XML データを Outlook 予定オブジェクトにインポートする (Outlook)

このトピックでは XML 形式で設定された予定のデータを読み取り、そのデータを既定の予定表の Microsoft Outlook AppointmentItem オブジェクトに保存して、予定オブジェクトを配列で取得する方法を説明します。

| MVP ロゴ

|以下のコード例は、Helmut Obertanner が用意したものです。 Helmut は、Microsoft Visual Studio および Microsoft Office Outlook.| の Microsoft Office 開発ツールに関する専門知識を持つ Microsoft Most Valuable Professional です。

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 Developer Tools for Visual Studio を使用)。 コードの Application オブジェクトは で提供された、信頼済み Outlook ThisAddIn.Globals オブジェクトである必要があります。 Outlook PIA を使用してマネージド Outlook ソリューションを開発する方法の詳細については、「 Outlook プライマリ相互運用機能アセンブリ リファレンスへようこそ」を参照してください。 次のコード サンプルには、Outlook アドイン プロジェクトのSample一部として実装された クラスのメソッドが含まれていますCreateAppointmentsFromXml。 各プロジェクトは、 Microsoft.Office.Interop.Outlook 名前空間に基づく Outlook PIA への参照を追加します。 メソッドは CreateAppointmentsFromXmlapplicationxml という 2 つの入力パラメーターを受け入れます。

  • application は信頼された Outlook アプリケーション オブジェクトです。

  • xml は、XML 文字列、または妥当な XML ファイルのパスを表す文字列のどちらかです。 For the purpose of the following code samples, the XML delimits appointment data by using the following XML tags:

予定データ 区切りの 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 ドキュメント オブジェクト モデル (DOM) の Microsoft COM 実装を使用して、 xml が提供する XML データを読み込んで処理します。 CreateAppointmentsFromXml 最初に、 xml が XML データの有効なソースを指定しているかどうかを確認します。 If so, it loads the data into an XML document, DOMDocument. Otherwise, CreateAppointmentsFromXml throws an exception. XML DOM の詳細については、「 DOM」を参照してください。 XML データ内の予定タグで <区切られた予定> の子ノードごとに、特定のタグを検索し、 CreateAppointmentsFromXml DOM を使用してデータを抽出し、 AppointmentItem オブジェクトの対応するプロパティ ( StartEndSubjectLocationBody) にデータを割り当てます。 CreateAppointmentsFromXml then saves the appointment to the default calendar. 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 のサポートおよびフィードバックを参照してください。