外部自动化方案和示例

Microsoft Office InfoPath 主互操作程序集 (Microsoft.Office.Interop.InfoPath.dll) 和 InfoPath XML 互操作程序集提供的成员 (Microsoft.Office.Interop.InfoPath.Xml.dll) 支持编写托管代码以自动执行 InfoPath。

建立对 Microsoft Office InfoPath 主互操作和 InfoPath XML 互操作程序集的引用

若要编写用于自动执行 InfoPath 的托管代码,必须建立对 Microsoft InfoPath 主互操作和 InfoPath XML 互操作程序集的引用。 Microsoft InfoPath 主互操作程序集通过使用 Microsoft.Office.Interop.InfoPath 命名空间的成员,支持与 IPEDITOR.DLL 公开的 COM 对象模型的互操作性。 InfoPath XML 互操作程序集通过使用 Microsoft.Office.Interop.InfoPath.Xml命名空间的成员 ,支持与 Microsoft XML Core Services (MSXML) 公开的 COM 对象模型的互操作性。

重要

自动执行 InfoPath 的托管代码应用程序的用户必须在其计算机上安装 InfoPath、Microsoft Office InfoPath 主互操作程序集和 InfoPath XML 互操作程序集。 对于 InfoPath 的典型安装,InfoPath 安装程序中的 .NET 可编程性支持 选项设置为 “从我的计算机运行 ”。

因此,只要安装了 .NET Framework 1.1 可再发行组件或 .NET Framework 1.1 软件开发工具包 (SDK) 或更高版本,这些互操作程序集也会默认安装。 如果这些互操作程序集在用户的计算机上不可用,则必须确认已安装 .NET Framework 1.1 或更高版本,然后从 控制面板运行程序和功能以更改设置,并将 InfoPath 的 .NET 可编程性支持选项设置为从我的计算机运行

以下过程介绍如何在 Visual Studio 项目中设置对 Microsoft Office InfoPath 主互操作和 InfoPath XML 互操作程序集的引用。

若要设置对 Microsoft.Office.Interop.InfoPath 主互操作程序集的引用,请在“添加引用”对话框的“COM”选项卡上设置对 Microsoft InfoPath 3.0 类型库的引用。 即使 从 COM 选项卡 设置了引用,也会建立对 InfoPath 安装程序安装在全局程序集缓存 (GAC) 中的 Microsoft.Office.Interop.InfoPath.dll 主互操作程序集的引用。

设置对 Microsoft.Office.Interop.InfoPath 主互操作程序集的引用

  1. 打开 Visual Studio 托管代码项目。

  2. 在“解决方案资源管理器”中,右键单击“引用”,然后单击“添加引用”

  3. 在“ COM ”选项卡上,双击“ Microsoft InfoPath 3.0 类型库”,然后单击“ 确定”。

若要设置对 Microsoft.Office.Interop.InfoPath.Xml 互操作程序集的引用,请浏览到驱动器>:\Program Files\Microsoft Office\OFFICE14 文件夹中默认<安装的 Microsoft.Office.Interop.InfoPath.Xml.dll 文件。 即使在本地文件系统中指定程序集的副本,此过程也会建立对 InfoPath 安装程序在全局程序集缓存 (GAC) 中安装的 Microsoft.Office.Interop.InfoPath.Xml.dll 程序集的引用。

设置对 Microsoft.Office.Interop.InfoPath.Xml 互操作程序集的引用

  1. 打开或创建 Visual Studio 托管代码项目,例如 控制台应用程序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. 在“ 详细信息 ”选项卡上,选择并复制 “命名空间:”下面的值,然后将其粘贴到记事本或其他可检索的位置。 稍后需要使用此值在代码中设置 SelectionNamespaces 属性的值。

  5. 将表单模板发布到名为 C:\Test 的文件夹,并接受默认名称 Template1。

  6. 打开表单模板,将名称“公司 A”添加到绑定到 customerName 字段的文本框,然后将表单另存为“Form1”。

创建托管代码控制台应用程序,将名称从“公司 A”更改为“公司 B”

  1. 打开 Visual Studio 并创建名为 UpdateCustomer 的新 Visual C# 或 Visual Basic 控制台应用程序。

  2. 如上所述,建立对 Microsoft Office InfoPath 主互操作和 InfoPath XML 互操作程序集的引用。

  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. 在“ 详细信息 ”选项卡上,选择并复制 “命名空间:”下面的值,然后将其粘贴到记事本或其他可检索的位置。 稍后需要使用此值在代码中设置 SelectionNamespaces 属性的值。

  5. 将表单模板发布到名为 C:\Temp 的文件夹,并接受默认名称 Template1。

  6. 打开表单模板,将空白表单另存为“Form1”到 C:\Temp。

创建托管代码控制台应用程序以打开窗体并填充字段

  1. 打开 Visual Studio 并创建名为 OpenForm 的新 Visual C# 或 Visual Basic 控制台应用程序。

  2. 如上所述,建立对 Microsoft Office InfoPath 主互操作和 InfoPath XML 互操作程序集的引用。

  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”和“地址”字段,然后保存表单,使 InfoPath 保持打开状态。

另请参阅