none
How to invoke a built in ribbon command in outlook 2007/2010 RRS feed

  • Question

  • Hello everyone,

     

    Anyone knows how to invoke programmatically a built-in ribbon command?

    Basically, I can't seem to find a reference to the built in ribbon tabs.

    Thanks in advance,

    Jason


    Jason Orphanidis
    Thursday, February 2, 2012 3:35 PM

Answers

All replies

  • You can try to use Explorer.CommandBars and Inspector.CommandBars to find the button (it is still there even if Outlook now shows a ribbon) - you can see it in OutlookSpy - click Explorer or Inspector button on the OutlookSpy toolbar and go to the CommandBars tab.

    If using Redemption is an option, the next version (5.3) will expose SafeInspector.Ribbon and SafeExplorer.Ribbon objects that allow to enumerate and invoke ribbon controls. Let me know if you want a beta version.

    'simulate Send/Receive
    set sExplorer = CreateObject("Redemption.SafeExplorer")
    sExplorer.Item = Application.ActiveExplorer
    set Ribbon =  sExplorer.Ribbon
    oldActiveTab = Ribbon.ActiveTab
    Ribbon.ActiveTab = "Send / Receive"
    set Control = Ribbon.Controls("Send/Receive All Folders")
    Control.Execute
    Ribbon.ActiveTab = oldActiveTab 'restore the active tab

     


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.2 is now available!
    Thursday, February 2, 2012 3:54 PM
  • Thanks for your input Dmitry.

    I downloaded OutlookSpy and found it very useful!

    I managed to do what I wanted via the Inspector.CommandBars collection, however I can't really say that I accessed the ribbon.

    The CommandBars collection contains a Ribbon reference but it is empty with no controls in it. I am not sure if I can typecast it (queryinterface) into something else and get a hold on tabs and controls from there, but from what I can see it's a dead end.

    Instead, I used the "MenuBar" CommandBar which holds the old Outlook 2003 menu structure and luckily I found the ribbon command I wanted to execute was in there somewhere. If this wasn't the case, I am not sure how I could do it.

    So, I guess the question of how to access a ribbon command - without using Redemption, which looks very useful as I can see from your sample code - remains unanswered, unless the answer is "you can't".


    Jason Orphanidis
    Friday, February 3, 2012 10:13 AM
  • There is ExecuteMso function that will do it, providing you know the name of button (you can learn name/id of button by using Customize Ribbon command and hovering over target element untill tooltip appears). http://msdn.microsoft.com/en-us/library/ff862419.aspx

    Friday, February 3, 2012 10:41 AM
  • That's it.

    Thanks!


    Jason Orphanidis
    Friday, February 3, 2012 10:50 AM
  • Keep in mind that ExecuteMso is Office 2010 specific, it is not available in Office 2007.

     


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.2 is now available!
    Friday, February 3, 2012 1:48 PM
  • It is available via Inspector.CommandBars instead of Application.CommandBars.

     


    Jason Orphanidis
    Friday, February 3, 2012 1:51 PM
  • In Office 2007???

     


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.2 is now available!
    Friday, February 3, 2012 2:05 PM
  • Yes!

    See the interface definition: http://msdn.microsoft.com/en-us/library/aa433403(v=office.12).aspx

     


    Jason Orphanidis
    Friday, February 3, 2012 2:08 PM
  • Hello,

    I'm developing a VSTO addin for Outlook 2010 (with VS 2010 fwk 4), and I want to invoke an add-in which is in the ribbon tab "Add-ins" (I did somethings like that with Outlook 2003), I tried to do your solution but I did not find an object "CommandBars" into Application object, neither in inspector or explorer objects...

    ==================

    <Global.System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Tools.Office.ProgrammingModel.dll", "10.0.0.0")> _
    Friend WithEvents Application As Microsoft.Office.Interop.Outlook.Application

    ==================

    any ideas??

    thanks a lot,

    Mauricio.


    mpv

    Friday, February 24, 2012 2:05 PM
  • What is your code that did not work?

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.2 is now available!

    Friday, February 24, 2012 2:07 PM
  • I'm testing with this:

    Protected Friend Sub btnRightFax()
     Dim cBars As Office.CommandBars
     cBars = Applicaton.CommandBars   

    cBars.ExecuteMso("XX")
    End Sub

    CommandBars object is not available in Application object


    mpv

    Friday, February 24, 2012 2:18 PM
  • Application object does have the CommandBars property. Explorer and Inspector objects do.

    Use Application.ActiveExplorer or Application.ActiveInspector. Keep in mind that both objects can return null if there are not visible explorers or inspectors.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.2 is now available!

    Friday, February 24, 2012 2:29 PM
  • that's my problem, it did not appear in my intellisense...

    object browser: application

    It did not appear for Inspector or Explorer objects too, but in Object Browser appear like disable with this warkning: "

    This object, member, or enumeration is deprecated and is not intended to be used in your code.

    "


    Mauricio

    Friday, February 24, 2012 2:39 PM
  • yes, you are right (obvius, je), my problem was that the VS IDE hide deprecated objects/methods, but if you know that then you can use this objects anyway.

    Now I'll try to get the "idMso" for that Addin.

    thanks!


    Mauricio

    Friday, February 24, 2012 2:50 PM
  • use something like:

    Application.ActiveExplorer().CommandBars.ExecuteMso(proper.id);

    Friday, February 24, 2012 2:52 PM
  • Hi DamianD,

    I could not find the "idMso" for my custom add-in, so I did this:

    =======================

                For i = 1 To cBars.Count
                    cBar = cBars.Item(i)
                    cCtrls = cBar.Controls
                    For j = 1 To cCtrls.Count
                        cCtrl = cCtrls.Item(j)
                        If cCtrl.Caption = "AddInRightFax" Then
                            cCtrlEx = cCtrl
                            Exit For
                        End If
                    Next
                    If Not cCtrlEx Is Nothing Then
                        Exit For
                    End If
                Next

                If Not cCtrlEx Is Nothing Then
                    cCtrlEx.Execute()
                End If

    =======================

    (I tried using FindControl method but did not work, I suppose because I don't have the correct id...)

    thanks.


    Mauricio

    Friday, February 24, 2012 6:28 PM
  • You cannot enumerate ribbon controls.

    Do you see the control in the "Standard" toolbar?

    Are you sure you have the right caption?


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.2 is now available!

    Friday, February 24, 2012 6:31 PM
  • no, this control appear in a custom toolbar..... now I realize that it is the answer because I did not found this control, it was beacuse I was finding in the wrong CommandBar....

    (yes, I have the right caption, I'm using it to find the right control)

    thanks a lot!


    Mauricio

    Friday, February 24, 2012 6:55 PM
  • Hi Mauricio,

    I'm in the same situation. I have a custom ribbon tab and I'm trying to execute one of the controls on this tab.

    The code in use is as follows:

    CommandBars.ExecuteMso("CUSTOM_CONTROL_ID")

    However it is unable to find the control.

    Did you manage to solve this problem?

    Thanks.

    Wednesday, April 11, 2012 2:04 PM
  • Hi Mauricio,

    I'm in the same situation. I have a custom ribbon tab and I'm trying to execute one of the controls on this tab.

    The code in use is as follows:

    CommandBars.ExecuteMso("CUSTOM_CONTROL_ID")

    However it is unable to find the control.

    Did you manage to solve this problem?

    Thanks.

    Wednesday, April 11, 2012 2:04 PM
  • You cannot call ExecuteMso() on a custom ribbon control, only on a built-in ribbon control. If this is a button in your own code just call the method directly. For example, say the normal callback for a button is this:
     
    public void myCallback(IRibbonControl control)
    {
        doCallbackMethod();
    }
     
    private void doCallbackMethod()
    {
        // this does the actual work
    }
     
    You could just call doCallbackMethod() directly and solve the problem.

    --
    Ken Slovak
    MVP - Outlook
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
     
     
    "pd87" <=?utf-8?B?cGQ4Nw==?=> wrote in message news:8bec964d-02b9-414f-89f2-3f3e8be090d4...

    Hi Mauricio,

    I'm in the same situation. I have a custom ribbon tab and I'm trying to execute one of the controls on this tab.

    The code in use is as follows:

    CommandBars.ExecuteMso("CUSTOM_CONTROL_ID")

    However it is unable to find the control.

    Did you manage to solve this problem?

    Thanks.


    Ken Slovak MVP - Outlook
    Wednesday, April 11, 2012 2:37 PM
  • i do not think it is possible to use ExecuteMso to invoke ribbon customization from add-ins. Is then button you want to invoke from your own add-in?
    Wednesday, April 11, 2012 2:45 PM
  • Hi, here is my workaround:

    ==========================================================

        Protected Friend Sub btnRightFax()
            Try
                Dim cBars As Office.CommandBars, cCtrlEx As Office.CommandBarControl = Nothing
                Dim cCtrls As Office.CommandBarControls, cBar As Office.CommandBar = Nothing, cCtrl As Office.CommandBarControl
                cBars = moApplication.ActiveExplorer.CommandBars
    
                For i = 1 To cBars.Count
                    cBar = cBars.Item(i)
                    cCtrls = cBar.Controls
                    'Diagnostics.Debug.WriteLine(cBar.Name)
                    For j = 1 To cCtrls.Count
                        cCtrl = cCtrls.Item(j)
                        'Diagnostics.Debug.WriteLine("---" & cCtrl.Caption & "(" & j & ")")
                        'Diagnostics.Debug.WriteLine("---" & cCtrl.Id)
                        'Diagnostics.Debug.WriteLine("---" & cCtrl.Tag)
                        If cCtrl.Caption = "AddInRightFax" Then
                            cCtrlEx = cCtrl
                            Exit For
                        End If
                    Next
                    If Not cCtrlEx Is Nothing Then
                        Exit For
                    End If
                Next
                If Not cCtrlEx Is Nothing Then
                    cCtrlEx.Execute()
                Else
                    Throw New Exception("No se encontró el addin 'AddInRightFax' en las CommandBars.")
                End If
                'cCtrl = cBar.FindControl(Office.MsoControlType.msoControlButton, 1)
                'cCtrl.Execute()
                'cBars.ExecuteMso("Add-In-RightFax")
            Catch ex As Exception
                UT.Trace.WrError(ex.ToString, 132, UT.Trace.TracePriority.HIGH)
                Dim lErrH As New ErrorHand.DisplayError()
                lErrH.ShowError("Atención", "No fue posible ejecutar el RightFax addin, verifique que esté instalado.", DisplayError.Iconos.iExclamacion, ex.Message, ex.StackTrace)
            End Try
        End Sub

    ==========================================================

    
    

    cheers,

    Mauricio.


    Mauricio

    Wednesday, April 11, 2012 3:14 PM