共用方式為


外部自動化案例與範例

由 MicrosoftR Office InfoPath 主要 Interop 組件 (Microsoft.Office.Interop.InfoPath.dll) 以及 InfoPath XML Interop 組件 (Microsoft.Office.Interop.InfoPath.Xml.dll) 所公開的成員支援撰寫自動化 Microsoft Office InfoPath 2007 的 Managed 程式碼。

建立 Microsoft Office InfoPath 主要 Interop 與 InfoPath XML Interop 組件的參考

若要撰寫自動化 InfoPath 的 Managed 程式碼,必須先建立 Microsoft Office InfoPath 主要 Interop 與 InfoPath XML Interop 組件的參考。Microsoft Office InfoPath 主要 Interop 組件使用 Microsoft.Office.Interop.InfoPath 命名空間的成員,提供 IPEDITOR.DLL 所公開的 COM 物件模型的互通性支援。InfoPath XML Interop 組件則使用 Microsoft.Office.Interop.InfoPath.Xml 命名空間的成員,提供 Microsoft Office 的 Microsoft XML Core Services (MSXML) 5.0 所公開的 COM 物件模型的互通性支援。

重要

自動化 InfoPath 的 Managed 程式碼應用程式的使用者必須在其電腦上安裝 Office InfoPath 2007、Microsoft Office InfoPath 主要 Interop 組件,以及 InfoPath XML Interop 組件。在 Office InfoPath 2007 安裝程式中,InfoPath 一般安裝的 [.NET 程式控制支援] 選項會設為 [從我的電腦執行]。因此只要有安裝 .NET Framework 1.1 轉散發套件或 .NET Framework 1.1 Software Development Kit (SDK),依據預設也會安裝這些 Interop 組件。如果在使用者的電腦上沒有這些 Interop 組件,您必須確認已安裝 .NET Framework 1.1,然後從 Office InfoPath 2007 [控制台] 執行 [新增或移除程式],並將 Office InfoPath 2007 的 [.NET 程式控制支援] 選項設為 [從我的電腦執行]。

下列程序說明如何設定 Visual Studio .NET 專案中的 Microsoft Office InfoPath 主要 Interop 以及 InfoPath XML Interop 組件的參考。

若要設定 Microsoft.Office.Interop.InfoPath 主要 Interop 組件的參考,請在 [加入參考] 對話方塊的 [COM] 索引標籤上,設定 [Microsoft InfoPath 2.0 型別程式庫] 的參考。即使您從 [COM] 索引標籤設定參考,仍會建立 InfoPath 安裝程式在「全域組件快取」(GAC) 中安裝的 Microsoft.Office.Interop.InfoPath.dll 主要 Interop 組件的參考。

設定 Microsoft.Office.Interop.InfoPath 主要 Interop 組件的參考

  1. 開啟 Visual Studio Managed 程式碼專案。

  2. 在 [方案總管] 中,用滑鼠右鍵按一下 [參考],再按一下 [加入參考]。

  3. 在 [COM] 索引標籤上按兩下 [Microsoft InfoPath 2.0 型別程式庫],然後按一下 [確定]。

若要設定 Microsoft.Office.Interop.InfoPath.Xml Interop 組件的參考,請瀏覽至預設安裝在 <drive>:\Program Files\Microsoft Office\OFFICE12 資料夾中的 Microsoft.Office.Interop.InfoPath.Xml.dll 檔案。即使您在本機檔案系統中指定組件的副本,此程序仍會建立 InfoPath 安裝程式在「全域組件快取」(GAC) 中安裝之 Microsoft.Office.Interop.InfoPath.Xml.dll 組件的參考。

設定 Microsoft.Office.Interop.InfoPath.Xml Interop 組件的參考

  1. 開啟或建立 Visual Studio Managed 程式碼專案,例如 [主控台應用程式] 或 [Windows 應用程式]。

  2. 在 [方案總管] 中,用滑鼠右鍵按一下 [參考],再按一下 [加入參考]。

  3. 在 [.NET] 索引標籤上按一下 [瀏覽],瀏覽至 <drive>:\Program Files\Microsoft Office\OFFICE12 資料夾,然後按一下 Microsoft.Office.Interop.InfoPath.Xml.dll。

  4. 按一下 [確定]。

自動化變更欄位值

假設一位 InfoPath 銷售報表表單範本使用者的顧客,最近將範本名稱從 "Company A" 改為 "Company B"。開發人員收到撰寫程式碼的要求,以自動更新從此表單範本儲存的銷售報表表單,來反映名稱的變更。下列案例假設一個表單包含一個繫結至欄位的文字方塊,此欄位的名稱為 customerName

建立範例表單範本與表單

  1. 開啟 InfoPath 並建立空白表單範本。

  2. 將 [文字方塊] 控制項新增至表單中,並將繫結至控制項的欄位命名為 customerName

  3. 將表單範本發佈至名為 C:\Test 的資料夾,並接受預設名稱:Template1。

  4. 開啟表單範本,並將名稱 "Company A" 新增至與 customerName 欄位繫結的文字方塊,然後將表單儲存為 "Form1"。

建立 Managed 程式碼主控台應用程式,將名稱從 'Company A' 變更為 'Company B'

  1. 開啟 Visual Studio,並建立名為 UpdateCustomer 的新 Visual C# 或 Visual Basic 主控台應用程式。

  2. 建立 Microsoft Office InfoPath 的主要 Interop 與 InfoPath XML Interop 組件的參考,如上所述。

  3. 將下列程式碼加入 Program.cs 檔案或 Module1.vb 檔案:

    using System;
    using System.Collections.Generic;
    using System.Text;
    using Microsoft.Office.Interop.InfoPath;
    using Microsoft.Office.Interop.InfoPath.Xml;
    
    namespace UpdateCustomer
    {
       class Program
       {
          static void Main(string[] args)
          {
    
             // Create an InfoPath Application object.
             Application myApp = 
                new Microsoft.Office.Interop.InfoPath.Application();
    
             // Get a reference the XDocuments collection 
             // and open the sample form.
             XDocumentsCollection myXDocs = myApp.XDocuments;
             XDocument myXDoc = myXDocs.Open("C:\\Test\\Form1.xml",
                (int) XdDocumentVersionMode.xdFailOnVersionOlder);
    
             // Access the XML DOM for the underlying XML document using
             // the DOM property. Note that you must cast to the 
             // IXMLDOMDocument2 type from the
             // Microsoft.Office.Interop.InfoPath.Xml namespace
             // to access the XML DOM.
             IXMLDOMDocument2 myXMLDoc = myXDoc.DOM as IXMLDOMDocument2;
    
             // Set the MSXML SelectionNamespaces property to the my
             // namespace of the form. IMPORTANT:Replace the namespace 
             // alias with that of your sample form.
             myXMLDoc.setProperty("SelectionNamespaces",
    "xmlns:my='https://schemas.microsoft.com/office/infopath/2003/myXSD/2006-09-06T23:17:34'");
    
             // Select all instances of customerName that contain 
             //'Company A'.
             IXMLDOMNodeList myNames = 
                myXMLDoc.selectNodes(
                "//my:customerName[. = 'Company A']");
    
             // Check to determine if any nodes were returned.
             if (myNames.length < 1)
             Console.WriteLine(
                "No elements containing 'Company A' were found.");
    
             // Loop through the list updating to 'Company B'.
             IXMLDOMNode myName = myNames.nextNode();
    
             while (myName != null)
             {
                myName.text = "Company B";
                myName = myNames.nextNode();
    
             }
    
             // Save the updated form as Form2.xml and close out.
             myXDoc.SaveAs("C:\\Test\\Form2.xml");
             myXDocs.Close(0);
             myApp.Quit(false);
             Console.WriteLine("Finished!");
    
          }
       }
    }
    
    Imports Microsoft.Office.Interop.InfoPath
    Imports Microsoft.Office.Interop.InfoPath.Xml
    Module Module1
       Sub Main()
          ' Create an InfoPath Application object.
          Dim myApp As Application = _
             New Microsoft.Office.Interop.InfoPath.Application()
    
          ' Get a reference the XDocuments collection 
          ' and open the sample form.
          Dim myXDocs As XDocumentsCollection = myApp.XDocuments
          Dim myXDoc As XDocument = myXDocs.Open( _
             "C:\\Test\\Form1.xml", _
             XdDocumentVersionMode.xdFailOnVersionOlder)
    
          ' Access the XML DOM for the underlying XML document using
          ' the DOM property. Note that you must cast to the 
          ' IXMLDOMDocument2 type from the
          ' Microsoft.Office.Interop.InfoPath.Xml namespace
          ' to access the XML DOM.
          Dim myXMLDoc As IXMLDOMDocument2 = _
             DirectCast(myXDoc.DOM, IXMLDOMDocument2)
    
          ' Set the MSXML SelectionNamespaces property to the my
          ' namespace of the form. IMPORTANT:Replace the namespace 
          ' alias with that of your sample form.
          myXMLDoc.setProperty("SelectionNamespaces", _
    "xmlns:my='https://schemas.microsoft.com/office/infopath/2003/myXSD/2006-09-06T23:17:34'")
    
          ' Select all instances of customerName that contain 
          ''Company A'.
          Dim myNames As IXMLDOMNodeList = _
       myXMLDoc.selectNodes("//my:customerName[. = 'Company A']")
    
          ' Check to determine if any nodes were returned.
          If (myNames.length < 1) Then
             Console.WriteLine( _
                "No elements containing 'Company A' were found.")
          Else
             ' Loop through the list updating to 'Company B'.
             Dim myName As IXMLDOMNode = myNames.nextNode()
             While (myName IsNot Nothing)
                myName.text = "Company B"
                myName = myNames.nextNode()
             End While
          End If
    
          ' Save the updated form as Form2.xml and close out.
          myXDoc.SaveAs("C:\\Test\\Form2.xml")
          myXDocs.Close(0)
          myApp.Quit(False)
          Console.WriteLine("Finished!")
       End Sub
    End Module
    
  4. 按一下 [偵錯] 功能表中的 [開始偵錯],編譯並執行主控台應用程式。

    應用程式會開啟儲存名稱為 Form1.xml 的表單,並對所有包含 Company A 值的 customerName 元素執行迴圈,並將值變更為 Company B。完成作業後,新的表單副本就會在 C:\Test 資料夾中儲存成 Form2.xml

    注意

    如果要判斷表單範本的 xmlns:my 命名空間屬性以設定 SelectionNamespaces 屬性,請在 InfoPath 中開啟表單範本,開啟 [資料來源] 工作窗格,用滑鼠右鍵按一下 [customerName] 欄位,然後按一下 [屬性]。命名空間的值會顯示在 [詳細資料] 索引標籤上。您也可以在文字編輯器中開啟 Form1.xml 檔案,以判斷這個值。

自動化開啟表單與填入欄位值

下面的範例會自動開啟空白表單並填入名字、姓氏及地址欄位。此案例假設表單包含三個文字方塊,且文字方塊繫結至名為 [FirstName][LastName][Address] 的欄位。

建立範例表單範本與表單

  1. 開啟 InfoPath 並建立空白表單。

  2. 新增三個文字方塊控制項至表單,並將繫結至控制項的欄位命名為:FirstName、LastName 及 Address。接著新增任何您想要的其他欄位。

  3. 將表單範本發佈至名為 C:\Temp 的資料夾,並接受預設名稱:Template1。

  4. 開啟表單範本,並將空白表單命名為 "Form1" 儲存至 C:\Temp。

建立 Managed 程式碼主控台應用程式,以開啟表單和填入欄位

  1. 開啟 Visual Studio,並建立名為 OpenForm 的新 Visual C# 或 Visual Basic 主控台應用程式。

  2. 建立 Microsoft Office InfoPath 的主要 Interop 與 InfoPath XML Interop 組件的參考,如上所述。

  3. 將下列程式碼加入 Program.cs 檔案或 Module1.vb 檔案:

    using System;
    using System.Collections.Generic;
    using System.Text;
    using Microsoft.Office.Interop.InfoPath;
    using Microsoft.Office.Interop.InfoPath.Xml;
    
    namespace OpenForm
    {
       class Program
       {
          static void Main(string[] args)
          {
    
             // Create an InfoPath Application object.
             Application myApp=
                new Microsoft.Office.Interop.InfoPath.Application();
    
             // Create an InfoPath XDocument variable and open 
             // the blank form.
             XDocument myXDoc = myApp.XDocuments.Open(
                "c:\\temp\\Form1.xml",
                (int) XdDocumentVersionMode.xdFailOnVersionOlder);
    
             // Create an IXMLDOMDocument2 variable and access 
             // the XML DOM from the underlying XML document
             // using the DOM property of the XDocument object. 
             // Note that you must cast to IXMLDOMDocument2 to do so.
             IXMLDOMDocument2 doc= myXDoc.DOM as IXMLDOMDocument2;
    
             // Set the MSXML SelectionNamespaces property to the my
             // namespace of the form. IMPORTANT:Replace the namespace
             // with that of your sample form.
             doc.setProperty("SelectionNamespaces","xmlns:my='https://schemas.microsoft.com/office/infopath/2003/myXSD/2006-09-06T23:17:34'");
    
             // Pre-populate the fields with specified values.
             doc.selectSingleNode("//my:FirstName").text="My Name";
             doc.selectSingleNode("//my:LastName").text="My LastName";
             doc.selectSingleNode("//my:Address").text="My Address";
    
             // Save the form, leaving InfoPath open.
             myXDoc.Save();
    
          }
       }
    }
    
    Imports Microsoft.Office.Interop.InfoPath
    Imports Microsoft.Office.Interop.InfoPath.Xml
    
    Module Module1
    
       Sub Main()
          ' Create an InfoPath Application object.
          Dim myApp As Application = _
             New Microsoft.Office.Interop.InfoPath.Application
    
          ' Create an InfoPath XDocument variable and open 
          ' the blank form.
          Dim myXDoc As XDocument = myApp.XDocuments.Open( _
             "c:\\temp\\Form1.xml", _
             XdDocumentVersionMode.xdFailOnVersionOlder)
    
          ' Create an IXMLDOMDocument2 variable and access 
          ' the XML DOM from the underlying XML document
          ' using the DOM property of the XDocument object.
          Dim doc As IXMLDOMDocument2 = myXDoc.DOM
    
          ' Set the MSXML SelectionNamespaces property to the my
          ' namespace of the form. IMPORTANT:Replace the namespace
          ' with that of your sample form.
          doc.setProperty("SelectionNamespaces", "xmlns:my='https://schemas.microsoft.com/office/infopath/2003/myXSD/2006-09-06T23:17:34'")
    
          ' Pre-populate the fields with specified values.
          doc.selectSingleNode("//my:FirstName").text = "My Name"
          doc.selectSingleNode("//my:LastName").text = "My LastName"
          doc.selectSingleNode("//my:Address").text = "My Address"
    
          ' Save the form, leaving InfoPath open.
          myXDoc.Save()
    
       End Sub
    
    End Module
    
  4. 在 [偵錯] 功能表中按一下 [開始偵錯],以編譯並執行主控台應用程式。

    應用程式會開啟儲存為 Form1.xml 的表單,並以程式碼中所指定的值填入 [FirstName]、[LastName] 及 [Address] 欄位中,然後儲存表單並維持 InfoPath 開啟。

    注意

    如果要判斷表單範例的 xmlns:my 命名空間屬性值,以設定 SelectionNamespaces 屬性,請以 InfoPath 開啟表單範本、開啟 [資料來源] 工作窗格,用滑鼠右鍵按一下 [FirstName] 欄位,然後按一下 [屬性]。命名空間的值會顯示在 [詳細資料] 索引標籤中。您也可以在文字編輯器中開啟 Form1.xml 檔案,以判斷值。

請參閱

概念

關於 Microsoft Office InfoPath 主要 Interop 組件
關於 InfoPath XML Interop 組件