Display a Custom Toolbar in InfoPath Forms

The Customize dialog box allows you to create a custom toolbar in Microsoft Office InfoPath 2007, but you cannot add custom commands to the toolbar—you can add only built-in InfoPath commands. It is possible, however, to add a custom toolbar that contains custom commands to your InfoPath form.

To create a toolbar with custom commands, you must edit the form definition file (.xsf) associated with a given form. To be able to edit the .xsf file of a form, you must first extract the form files from your form. To extract the form files, open the form template (an .xsn file) in design mode, click Save As Source Files on the File menu, and then select a folder to which the form files will be extracted.

After you have extracted the form files, you can modify the .xsf file by using any text or XML editor, such as Microsoft Notepad.

Bb251008.vs_Caution(en-us,office.12).gif  Caution
Edit .xsf files with care. If an invalid entry is made in an .xsf file, the form that the file is associated with may be left in an unusable state and you may have to correct the invalid entry before you can open the file in InfoPath.

You can create a custom toolbar by defining a toolbar element as a child element of a view element. The toolbar element contains two required attributes: caption and name. The caption attribute is used as the title of the toolbar. The name attribute is used to identify the toolbar within a view.

The button element is used to define buttons on the custom toolbar. The following table describes each attribute of a button element.

Name Required Description
action No Specifies an action of an editing component, using the syntax "NameOfEditingComponent::NameOfAction"
caption No Provides the caption displayed on the button
icon No Provides a Uniform Resource Locator (URL) to a bitmap (.bmp) or graphics interchange format (.gif) file, which is used for the button or menu item
name No Used to associate the OnClick event handler of the button with a scripting function
showIf No Specifies the editing context of the button
tooltip No Provides the ScreenTip text to be used for the button
xmlToEdit No Specifies the name of an xmlToEdit element, for which the button is used

The following example defines a toolbar named Sales Report, which contains three buttons. To associate script with these buttons, you must create an event handler that handles the OnClick event for each button.

  
    <xsf:toolbar caption="Sales Report" name="Sales Report">
       <xsf:button name="SalesByRegion" caption="Sales by Region"></xsf:button>
       <xsf:button name="SalesByMonth" caption="Sales by Month"></xsf:button>
       <xsf:button name="SalesByEmp" caption="Sales by Employee"></xsf:button>
    </xsf:toolbar>

You can use the menu element to create a drop-down menu on your custom toolbar. The following table describes the elements and attributes of a menu element.

Name Required Description
button No Defines a button that has an associated action
caption Yes Used as the caption for a menu
menu No Contains information about the menus used within a menu (cascading menus)
Bb251008.vs_note(en-us,office.12).gif  Note
To create cascading menus, a menu element can be nested inside another menu element.

The following example builds on the previous example by adding a drop-down menu with the caption Sales by Qtr to the toolbar. This menu contains four buttons. You must create an event handler for the OnClick event associated with each of these buttons for them to be functional.

      <xsf:toolbar caption="Sales Report" name="Sales Report">
       <xsf:button name="SalesByRegion" caption="Sales by Region"></xsf:button>
       <xsf:button name="SalesByMonth" caption="Sales by Region"></xsf:button>
       <xsf:button name="SalesByEmp" caption="Sales by Employee"></xsf:button>
       <xsf:menu caption="Sales by Qtr">
           <xsf:button name="Q1Sales" caption="Q1 Sales"></xsf:button>
           <xsf:button name="Q2Sales" caption="Q2 Sales"></xsf:button>
           <xsf:button name="Q3Sales" caption="Q3 Sales"></xsf:button>
           <xsf:button name="Q4Sales" caption="Q4 Sales"></xsf:button>
       </xsf:menu>
    </xsf:toolbar>
Bb251008.vs_note(en-us,office.12).gif  Note
A complete reference to the .xsf file, including all of its elements, attributes, and other entities, can be found in the InfoPath XSF Reference that is part of the 2007 Microsoft Office system XML Schema Reference, available for download from the Microsoft Developer Network (MSDN).