Add C# or Visual Basic to your existing form using VSTO

Have you ever created an InfoPath form to later discover that you have to write code in order to accomplish something you need?
If so, you have one of two options:
You can use VSTA to add managed code to your form.  It is included in the Office package, but it is turn off by default.  You must go the Add/Remove Programs and customize Office to install it.
-- OR --
You can use VSTO to add managed code to your form which is what this blog entry is about.
When you create an InfoPath Form Template using Visual Studio 2005, you start with the Design a Form dialog.  On the left you’ll see:
Choosing one of these options will allow you to create a project using an existing form template.  For example, if you wanted to use a form template from a SharePoint site, you would choose the second option.  You can also choose the last option to import an Excel workbook or a Word document as your form template.
If you've selected an existing form template, you will get a message:
If you've selected a form template that already had code behind it, the existing code will be removed from the new form template as well.
VSTO will now create a project based on the form you've selected and you're ready to add code!
- Gary
Software Development Engineer
Comments (4)

  1. bkleynbok says:

    My question is on

    InfoPath ActiveX component that allows access to the InfoPath object.

    Is it a part of the VSTO or is it planned to be released separately in the near future.

    Reason for my inquiry is simple.

    InfoPath 2007 now offers Export to PDF feature which I would like to take full advantage of and to automate it.

    How would I go about doing it.

    Best Regards,


  2. Alnur Ali [MSFT] says:

    You actually shouldn’t need to use the InfoPath ActiveX component if you want to externally automate the export to PDF action.

    You can still externally automate InfoPath 2007 just like you could with 2003.  Here’s a snippet in jscript that should get you about what you want:

    var appObj = new ActiveXObject("InfoPath.Application");

    appObj.XDocuments.NewFromSolution("C:\Documents and Settings\alnurali\Desktop\Template20.xsn");

    appObj.XDocuments.Item(0).View.Export("C:\Documents and Settings\alnurali\Desktop\Template20CurrentView.PDF", "PDF");

    Note, however, that you may need to add a timer between loading the form and exporting the view.  This is because – depending on the complexity of your form – the view may not have fully loaded yet (so any export will of course fail).

    You can, of course, do the same thing using our ActiveX component, if that’s what your needs call for.  Again, you just need to call into the InfoPath object model from your host.  Here’s an example in managed code this time:

    formControl1.XmlForm.CurrentView.Export(@"C:Documents and SettingsalnuraliDesktopTemplate12CurrentViewPDF.pdf", Microsoft.Office.InfoPath.ExportFormat.Pdf);

    The same caution that I mentioned above about waiting for the view to load applies.

  3. bkleynbok says:

    How can I know for sure that form is loaded into formControl1?

    There is no event that fires or property being set.

    Is there a way to monitor something on the formControl ?

    -Boris Kleynbok

  4. Alnur Ali [MSFT] says:

    There is no property explicitly for this.

    You can try a few things

    1) check if the XDocument property != null. You will need to implement some sort of polling/callback mechanism for this yourself.

    2) handle the OnLoad or OnViewSwitch events from your host application (using the IConnectionPointContainer interface for C++ and delegates for .NET).

    .NET snippet:

    namespace IPHosted


       public partial class Form1 : Form


           public Form1()



               // sync to the startup event where you can register for individual events

               formControl1.InternalStartup += new Microsoft.Office.InfoPath.FormControl.EventHandler<EventArgs>(formControl1_InternalStartup);


           void formControl1_InternalStartup(object sender, EventArgs e)


    // sync to the View switch event (you can also sync to other form events, such as Save, Merge, etc)

               ((FormControl)sender).EventManager.FormEvents.ViewSwitched += new ViewSwitchedEventHandler(OnSwitchView);

    3) register to receive a callback during the Init event. This event fires before document OnLoad.

    There should be documentation on MSDN for the details of this. Basically you need to implement IInitEventHandler interface and call its SetInitEventHandler passing in pointer to your callback function.

Skip to main content