Programmatically Opening InfoPath 2007 Form - InfoPath Dev
in

InfoPath Dev

Programmatically Opening InfoPath 2007 Form

Last post 04-03-2014 11:52 AM by nilagiri. 8 replies.
Page 1 of 1 (9 items)
Sort Posts: Previous Next
  • 02-02-2007 11:13 AM

    Programmatically Opening InfoPath 2007 Form

    Completely new to the InfoPath World.

    Trying to programmatically open an InfoPath 2007 form and programmatically populate values, all in Visual Studio 2005 using c#.

    Keep seeing reference to 'thisApplication' in the examples, but am confused as how to instantiate.

    Any guidance would be greatly appreciated.

    In fact, if anybody has link to sample code that demonstrates the above, that would be wonderful.

    Thanks in advance,
    Rob Vettor
  • 02-02-2007 11:29 AM In reply to

    Re: Programmatically Opening InfoPath 2007 Form

    Hi Rob and welcome to our forum!
    I have sent e-mail out to our developers and hopefully we'll have an answer soon. This is an easy question. You need to use the InfoPath object model and once you have added a reference to that, you should be able to reference it. There is a method called NewFromSolution I think that allows you to specify the solution with an xml file that will prepopulate.
    Patrick Halstead
    Project Manager at Qdabra
  • 02-02-2007 11:37 AM In reply to

    • jimbob
    • Top 500 Contributor
    • Joined on 11-25-2004
    • USA
    • Posts 32

    Re: Programmatically Opening InfoPath 2007 Form

    'thisApplication' is assigned inside the _Startup method which is generated when you create a new InfoPath project.

    public void _Startup(Microsoft.Office.Interop.InfoPath.SemiTrust.Application app, XDocument doc)
    {
    thisXDocument = doc;
    thisApplication = app;

    // You can add additional initialization code here.
    }
    Jim Cantwell
    InfoPathDev - Redmond
  • 02-02-2007 12:32 PM In reply to

    Re: Programmatically Opening InfoPath 2007 Form

    Patrick and Jim...

    Thk for the quick response.

    When you say, "Create an InfoPath Project," are you referring to an InfoPath form template project (VS 2005, under Visual C#, Office, ...)?

    If so, not seeing the _StartUp Method.

    Again, thx in advance for your help.

    (by the way, do you know of a good example that might demonstrate how to programmatically open form in managed code and populate values?)

  • 02-03-2007 12:11 PM In reply to

    Re: Programmatically Opening InfoPath 2007 Form

    Hello Rob,

    InfoPath 2007 comes with a new Object Model so by default, you will not see thisXDocument or thisApplication. It is still possible to use the old 2003 OM if you choose to. However, before going into this, could you please tell me how your code will be interacting with InfoPath? In IP2007, there are three ways:

    1. Your code is running of an InfoPath form.
    2. Your code is running in a separate application and you externally automate InfoPath.
    3. (new in IP2007) Your code is running in a WinForms application and you are hosting an InfoPath control.

    For #1, you have an option of either using the old OM (make sure you design a form without checking the "Enable browser-compatible features only", then go to Tools |Form Options | Programming and select "C# (InfoPath 2003 Compatible)" in the Form Template code language dropdown. Then the generated code will contains thisApplication.
    On the other hand, it is preferable to use the new OM. Then you will be using this.Application instead of thisApplication (this will refer to the XmlFormHost object instance). However I am not sure how to open a new form in the new OM (check the MSDN documentation for IP2007 here:http://msdn2.microsoft.com/en-us/library/bb187369.aspx)

    For #2, create a simple .NET application, add references to Microsoft.Office.Interop.InfoPath and Microsoft.Office.Interop.InfoPath.Xml and then you can launch InfoPath, open a form and edit a field value in three lines of code:
    ApplicationClass infoPathApplication = new ApplicationClass();
    XDocument document = infoPathApplication.XDocuments.NewFromSolution(yourXsnPath);
    document.DOM.selectSingleNode(someXPath).text = someValue;

    For #3, add a Microsoft.Office.InfoPath.FormControl to your WinForm. You can then call NewFromFormTemplate() on the form control to load an XSN (you can provide a stream to your XML instance as well) and then you can programmatically change the values via formControl.XmlForm.MainDataSource.

    Hope that helps!
    David Airapetyan / Software Services Architect
    Qdabra® Software / Streamline data gathering to turn process into knowledge
  • 02-15-2007 03:26 PM In reply to

    Re: Programmatically Opening InfoPath 2007 Form

    Rob,

    It is possible to do what you're trying to do without using the 2003 Compatible OM.

    //get the path to your new form.
    string strNewFormPath = "C:\YourNewForm.xsn";

    XmlForm objNewForm = this.Application.XmlForms.NewFromFormTemplate(strNewFormPath);

    //Find the node in the new form that you want to set
    XPathNavigator objTargetField = objNewForm.MainDataSource.CreateNavigator().SelectSingleNode("//whatever the xpath is"), objNewForm.NamespaceManager);

    //Set its value to whatever it needs to be
    objTargetField.SetValue("some value");


    Hope this helps.
  • 03-06-2007 06:58 AM In reply to

    Re: Programmatically Opening InfoPath 2007 Form

    I just tried and my this.Application doesn't match with the MSDN documentation, i.e. several documented properties and methods are unavailable. From InfoPath 2007, I picked Tools/Programming/Loading Event, which opens VSTA (VS2005). When I inspect this.Application, I only get these:

    Properties:
    - Environment
    - Name
    - User

    Methods:
    - Equals
    - GetHashCode
    - GetType
    - ToString

    I checked 'this' and it's indeed of the XmlFormHost type. I think what I'm doing is davidair's option #1, but it doesn't seem to work... Any ideas?
  • 09-08-2008 08:22 AM In reply to

    • mkoram
    • Not Ranked
    • Joined on 09-04-2008
    • Posts 2

    Re: Programmatically Opening InfoPath 2007 Form

    Hey David,

     I am facing a similar problem can you please guide me through the following issues:

     - I have an aspx page with some fields which will be populated with values from a database. At the end there is a button which when clicked should open an Infopath form with all the values from the aspx page. Can you please provide with any available code snippets!!!

    - Also, Is it possible to have a pop-up window or a panel in infopath form which will be visible only when a particular button is clicked? Because one of the fields in the infopath form will get its value based on the value selected from the pop-up window or the panel.

    I am relatively new in Infopath Development and I am stuck with this issue. Can you please help me out!!!

     Thanks a lot in advance!!!

  • 04-03-2014 11:52 AM In reply to

    Re: Programmatically Opening InfoPath 2007 Form

    Rob,

     Do I nedd to write this code on form load event or is there any other way?

    Can I develop any console app  to open up form from c drive and then update the field values?

    I do not want to use code behind since form does not support any code behind or data connections when we publish to email recipients.

     Please let me know if you have any idea to accomplish this task.

    Thank you.

    Nila
Page 1 of 1 (9 items)
Copyright © 2003-2019 Qdabra Software. All rights reserved.
View our Terms of Use.