Automating InfoPath from an External Application

Microsoft Office InfoPath 2007 offers application automation in the form of methods of the Application object and the XDocuments collection.

Overview of the Application and XDocument objects

The Application object contains the following methods used for automation:

Method Description
CacheSolution Examines the form template in the cache and, if necessary, updates it from the published location of the form template.
Quit Quits the Microsoft Office InfoPath application.
RegisterSolution Installs the specified Microsoft Office InfoPath form template.
UnregisterSolution Uninstalls the specified Microsoft Office InfoPath form template.

The XDocuments collection contains the following methods that can be used for external automation:

Method Description
Close method Closes the specified Microsoft Office InfoPath form.
New method Creates a new Microsoft Office InfoPath form.
NewFromSolution method Creates a new Microsoft Office InfoPath form based on the specified form template.
NewFromSolutionWithData method Creates a new Microsoft Office InfoPath form using the specified XML data and form template.
Open method Opens the specified Microsoft Office InfoPath form.

Office InfoPath 2007 can also be hosted as an XML editor within a custom application by using the XmlFormEditor Component Object Model (COM) control. For more information on using this control, see Hosting InfoPath as an XML editor in another application.

To use the Application object from an external application, you use the CreateObject function with the ProgID of the InfoPath application ("InfoPath.Application") to create an object variable that represents the InfoPath application. You can then use the XDocuments property to access the XDocuments collection and use its methods to open or create an InfoPath form. The following example demonstrates the creation of a reference to the Application object using the Microsoft Visual Basic for Applications (VBA) programming language:

  Dim objIP As Object

Set objIP = CreateObject("InfoPath.Application")

' Open an existing form objIP.XDocuments.Open ("C:\MyFolder\MyForm.xml")

' Create a new form based on a form template objIP.XDocuments.NewFromSolution ("C:\MyFolder\MyForm.xsn")

Bb251029.vs_note(en-us,office.12).gif  Note
Because the CreateObject function creates an object variable using late binding, automatic statement completion will not be available in the Visual Basic Editor. Refer to the links in the preceding tables for information about the correct calling syntax.