Executing menu and toolbar commands from the InfoPath Object Model

While the InfoPath 2003 SP-1 object model is quite extensive, it doesn’t provide all the functionality that is exposed through the InfoPath menus and toolbars. However, you are not limited by that fact. Using the CommandBars object, you can access any of the functionality available in menus and toolbars.

For example, the InfoPath object model exposes a Print method but not a PrintPreview method.  However, that doesn’t mean that you can’t access this functionality.  Using the CommandBars object, you can execute the Print Preview functionality available on the Standard toolbar like so:

Application.ActiveWindow.CommandBars(“Standard”).Controls(“Print Preview”).Execute();

The list of available command bars is on the View | Toolbars menu. If you want to access a menu item, use “Menu Bar” for the command bar name. The control name to use is the same name that appears on a menu or in the tooltip when the mouse is hovered over a toolbar button. (Please refer to MSDN (http://msdn.microsoft.com) for more information about the CommandBars object.)

Comments (5)

  1. Vasu says:

    Is there a way to automate the designer itself. I have many forms and am looking into easing the publishing process by automating it. I want to open InfoPath, select a publishing method and location (mostly http) and then publish it. I am using InfoPath SP1 and C# managed code.


  2. Joshua Bell [MSFT] says:

    The InfoPath design mode doesn’t support automation. However, "publishing" is really just an operation which sets some properties in the XSF file (within the template XSN) and then copies the XSN to the target location.

    (Unless you’re publishing to a Windows SharePoint Services Form Library, in which case InfoPath does do some fancy stuff.)

    So you should be able to write code which cracks an XSN (just a CAB), sets the appropriate settings in the XSF, re-CABs the XSN, then copies the file to your HTTP location using whatever protocol you want.

  3. infopather says:

    i wnat to make new commandbars which execute my functions…

    but, i cannot find Add method in commandbars object..

    i have read a good counting commandbars example on news group of infopath..


    BindingFlags flags = BindingFlags.GetProperty | BindingFlags.DeclaredOnly

    | BindingFlags.Public | BindingFlags.Instance;

    ObjectWrapper commandBars =


    int commandBarsCount = (int)commandBars.InvokeByName(

    "Count", // prop


    null, // arguments

    null); // Culture

    thisXDocument.UI.Alert("Got " + commandBarsCount.ToString() + "



    InvokeByName method have a solution ?

  4. Thomas Kleemann says:

    Try this:


    Imports Microsoft.Office.Core

    Dim IPcommandBars As ObjectWrapper = CType(_Application.ActiveWindow.CommandBars, ObjectWrapper)

    Dim flags As System.Reflection.BindingFlags

    flags = System.Reflection.BindingFlags.GetProperty Or System.Reflection.BindingFlags.GetProperty.DeclaredOnly Or System.Reflection.BindingFlags.GetProperty.Public Or System.Reflection.BindingFlags.GetProperty.Instance

    Dim commandBarsCount As Integer = IPcommandBars.InvokeByName("Count", flags, Nothing, Nothing)

    _XDocument.UI.Alert("Got " + commandBarsCount.ToString() + " commandBars")

    flags = System.Reflection.BindingFlags.InvokeMethod

    Dim customBar As CommandBar

    Dim args() As Object = {"SignForm", MsoBarPosition.msoBarTop, False, True}

    customBar = IPcommandBars.InvokeByName("Add", flags, args, Nothing)

    Dim customControl As CommandBarButton

    customControl = customBar.Controls.Add(MsoControlType.msoControlButton, 1)

    customControl.DescriptionText = "Sign form with SignDoc"

    customControl.Caption = "Sign form"

    customControl.TooltipText = "Sign form with SignDoc"

    Dim picture_stream As Stream

    Dim bm As Bitmap

    picture_stream = System.Reflection.Assembly.GetExecutingAssembly.GetManifestResourceStream("200.bmp")

    If Not (picture_stream Is Nothing) Then

    Dim img As Image

    customControl.Picture = ImageToPictureConverter.Convert(img.FromStream(picture_stream))


    End If

    customBar.Visible = True

    customControl.Tag = "btnSignForm"

    AddHandler customControl.Click, AddressOf btnSignForm_Click

    End Sub

    Sub btnSignForm_Click(ByVal Ctrl As Microsoft.Office.Core.CommandBarButton, ByRef CancelDefault As Boolean)

    ‘Fire code for button_click in toolbar

    End Sub

    Public NotInheritable Class ImageToPictureConverter

    Inherits System.Windows.Forms.AxHost

    Private Sub New()


    End Sub ‘New

    Public Shared Function Convert(ByVal image As System.Drawing.Image) As stdole.IPictureDisp

    Return CType(System.Windows.Forms.AxHost.GetIPictureDispFromPicture(image), stdole.IPictureDisp)

    End Function ‘Convert

    End Class ‘ImageToPictureConverter



  5. Rob says:

    Is there a way to see all the commandbar names? I am trying to set the pen wight for ink entry, however i do not know the syntax w/o the commandbar names.

    ie. this will turn the eraser on but i do not know how to change the pen type or the pen weight


Skip to main content