共用方式為


外部自動化案例和範例

Microsoft Office InfoPath 主要 Interop 元件 (Microsoft.Office.Interop.InfoPath.dll) 和 InfoPath XML Interop 元件所提供的成員 (Microsoft.Office.Interop.InfoPath.Xml.dll) 支援撰寫 Managed 程式代碼來自動化 InfoPath。

建立 Microsoft Office InfoPath 主要 Interop 和 InfoPath XML Interop 元件的參考

若要撰寫用於自動化 InfoPath 的 Managed 程式代碼,您必須建立 Microsoft InfoPath 主要 Interop 和 InfoPath XML Interop 元件的參考。 Microsoft InfoPath 主要 Interop 元件支援使用 Microsoft.Office.Interop.InfoPath 命名空間的成員,與 IPEDITOR.DLL 公開的 COM 物件模型互操作性。 InfoPath XML Interop 元件支援與 MSXML) 所公開的 COM 物件模型互操作性,方法是使用 Microsoft XML Core Services (Microsoft.Office.Interop.InfoPath.Xml命名空間的成員。

重要事項

自動化 InfoPath 的 Managed 程式代碼應用程式用戶必須已在其電腦上安裝 InfoPath、Microsoft Office InfoPath 主要 Interop 元件和 InfoPath XML Interop 元件。 InfoPath 安裝程式中的 [.NET 可程式性支援 ] 選項設定為 [ 從我的計算機 執行],以進行 InfoPath 的一般安裝。

因此,只要安裝 .NET Framework 1.1 可轉散發套件或 .NET Framework 1.1 軟體開發工具包 (SDK) 或更新版本,預設也會安裝這些 Interop 元件。 如果使用者的計算機上無法使用這些 Interop 元件,您必須確認已安裝 .NET Framework 1.1 或更新版本,然後從 控制台 執行 [程式和功能] 來變更安裝程式,並將 InfoPath 的 [.NET 可程式性支援] 選項設定為 [從我的電腦執行]

下列程序說明如何在Visual Studio項目中設定 Microsoft Office InfoPath 主要 Interop 和 InfoPath XML Interop 元件的參考。

若要設定 Microsoft.Office.Interop.InfoPath 主要 Interop 元件的參考,請在 [新增參考] 對話框的 [COM] 索引卷標上設定 Microsoft InfoPath 3.0 類型連結庫的參考。 即使您從 [COM] 索 引卷標設定參照,仍會建立 InfoPath 安裝程式所) 之全域程式集緩存 (GAC 中所安裝 Microsoft.Office.Interop.InfoPath.dll 主要 Interop 元件的參考。

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

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

  2. 方案總管 中,以滑鼠右鍵按兩下 [參考],然後按兩下 [新增參考]

  3. [COM] 索引標籤 上,按兩下 [Microsoft InfoPath 3.0 類型連結庫],然後按兩下 [ 確定]

若要設定 Microsoft.Office.Interop.InfoPath.Xml Interop 元件的參考,請瀏覽至預設安裝在磁碟驅動器>:\Program Files\Microsoft Office\OFFICE14 資料夾中的 < 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 索引 標籤上,按兩下 [ 流覽],流覽至 <磁碟驅動器>:\Program Files\Microsoft Office\OFFICE14 資料夾,然後按兩下 [Microsoft.Office.Interop.InfoPath.Xml.dll]。

  4. 按一下確定

自動變更欄位的值

假設 InfoPath 銷售報表表表表單範本的其中一位使用者最近將其名稱從「公司 A」變更為「公司 B」。系統會要求開發人員撰寫程式代碼,以自動更新從此窗體範本儲存的銷售報表表表表體,以反映名稱變更。 下列案例假設窗體包含系結至名為 customerName 之字段的文字框。

建立範例表單範本和表單

  1. 開啟 InfoPath 並建立空白窗體範本。

  2. 文本框 控件新增至窗體,並將系結至控件 customerName 的欄位命名。

  3. 在 [欄 ] 工作窗格中,以滑鼠右鍵按兩下 myFields 資料夾,然後按兩下 [ 屬性]

  4. 在 [ 詳細數據] 索 引標籤上,選取並複製下列 Namespace: 的值,然後將此值貼到 [記事本] 或一些您可以擷取它的其他位置。 您稍後需要此值,才能在程式碼中設定 SelectionNamespaces 屬性的值。

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

  6. 開啟窗體範本,將名稱 「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檔案,請務必將 SelectionNamespaces 屬性設定中的命名空間值更新為您在建立範例表單時所複製的值。

     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 
             // value below with that of your sample form.
             myXMLDoc.setProperty("SelectionNamespaces",
     "xmlns:my='http://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 
           ' value below with that of your sample form.
           myXMLDoc.setProperty("SelectionNamespaces", _
     "xmlns:my='http://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 的窗體,並迴圈查看包含公司 A 值的所有 customerName 元素,並將該值變更為公司 B。作業完成時,表單的新複本會儲存為 C:\Test 資料夾中的 Form2.xml。

自動開啟表單並填入域值

下列範例會自動開啟空白窗體,並在窗體中填入名字、姓氏和位址字段。 此案例假設窗體包含三個文本框,這些文本框系結至名為 FirstName、LastName 和 Address 的欄位。

建立範例表單範本和表單

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

  2. 將三個文本框控件新增至窗體,並將系結至控件的欄位命名為:FirstName、LastName 和 Address。 新增您想要的任何其他欄位。

  3. 在 [欄 ] 工作窗格中,以滑鼠右鍵按兩下 myFields 資料夾,然後按兩下 [ 屬性]

  4. 在 [ 詳細數據] 索 引標籤上,選取並複製下列 Namespace: 的值,然後將此值貼到 [記事本] 或一些您可以擷取它的其他位置。 您稍後需要此值,才能在程式碼中設定 SelectionNamespaces 屬性的值。

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

  6. 開啟表單範本,並將空白表單儲存為 「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檔案,請務必將 SelectionNamespaces 屬性設定中的命名空間值更新為您在建立範例表單時所複製的值。

     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
             // value below with that of your sample form.
             doc.setProperty("SelectionNamespaces","xmlns:my='http://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
           ' value below with that of your sample form.
           doc.setProperty("SelectionNamespaces", "xmlns:my='http://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 保持開啟。

另請參閱