Compartir a través de


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

  • Anonymous
    April 21, 2004
    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.
    Thanks
  • Anonymous
    April 21, 2004
    The comment has been removed
  • Anonymous
    April 29, 2004
    The comment has been removed
  • Anonymous
    June 02, 2004
    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))
    picture_stream.Close()
    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()
    MyBase.New(Nothing)
    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

    -----------------------------------------------
    Thomas.Kleemann@klinikum-ingolstadt.de
  • Anonymous
    July 31, 2013
    The comment has been removed