2

I need to do two things:

  1. Add buttons to a custom group on the "Home" tab of the Outlook 2013 ribbon using Outlook 2013 VBA.

    • Everything I found online refers to Excel or Word.
  2. Within the macro that runs when each button is clicked I want to be able to tell the name of the button that was clicked.

    • I want to have a variable number of buttons like "Do 1", "Do 2", "Do 3", ..., "Do X" and each of them would run the same macro/sub and within the macro/sub I can see the name of the button so I know what to do. Otherwise I'd have to create a sub/macro for each button and I'm trying to avoid that.
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
IMTheNachoMan
  • 5,343
  • 5
  • 40
  • 89
  • http://www.experts-exchange.com/Software/Office_Productivity/Office_Suites/MS_Office/A_11770-HOW-TO-Manipulating-Office-Ribbon-Bar-only-with-VBA.html – Maciej Los Jan 06 '15 at 23:23
  • Interesting way to add to the ribbon. That helps me with #1. Thanks! Any idea on #2? – IMTheNachoMan Jan 06 '15 at 23:28

4 Answers4

3

The only possible way is to develop an add-in. Outlook doesn't allow to customize the UI using VBA.

You may find the Walkthrough: Creating a Custom Tab by Using the Ribbon Designer page helpful.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
1

I think i found it!

For #1 see this: HOW TO: Manipulating Office Ribbon Bar only with VBA.

For #2 you need to add onAction subroutine in ribbon definition XML file.

<mso:button id="MyButtonIdentifier1" label="MyMacroLabel" imageMso="HyperlinksVerify" onAction="NameOfMyMacro" visible="true"/>

The definition of NameOfMyMacro should looks like:

Sub NameOfMyMacro(control As IRibbonControl)
    'here your logic
    Select Case control.Id
        Case "MyButtonIdentifier1"
           'call another subroutine ;)
        Case "MyButtonIdentifier2"
    End Select
End Sub
Maciej Los
  • 8,468
  • 1
  • 20
  • 35
0

There is a way to add a custom ribbon into Outlook 2013 with the help of ADODB.Stream

I'm working with this solution at work since some years - but I was not able to apply it also at home.

First of all I'm preparing a text file that has the XML-strukture in it:

Dim Stream As Object
Dim FSO As FileSystemObject
Dim tsZwischenspeicher As TextStream

Set Stream = CreateObject("ADODB.Stream")
Set FSO = CreateObject("Scripting.FileSystemObject")

strPfad = "C:\Users\" & (Environ("username")) & "\AppData\Local\Microsoft\Office\"
strSpeicherpfad = strPfad & "olkexplorer.officeUI"
strTempSpeicherpfad = strPfad & "olkexplorer.temp"

...

tsZwischenspeicher.WriteLine Anführungszeichen("<mso:customUI xmlns:x1='http://schemas.microsoft.com/office/2009/07/customui/macro' xmlns:mso='http://schemas.microsoft.com/office/2009/07/customui'>")
tsZwischenspeicher.WriteLine Anführungszeichen("<mso:ribbon>") & vbCrLf
tsZwischenspeicher.WriteLine Anführungszeichen("<mso:qat>")
tsZwischenspeicher.WriteLine Anführungszeichen("<mso:sharedControls>")
tsZwischenspeicher.WriteLine Anführungszeichen("<mso:control idQ='mso:FilePrint' visible='false'/>")

Then the generated XML-File could be transfered into Outlook via ADODB.Stream:

'Eine neue Fußzeile erstellen
tsZwischenspeicher.WriteLine Anführungszeichen("</mso:tabs>")
tsZwischenspeicher.WriteLine Anführungszeichen("</mso:ribbon>")
tsZwischenspeicher.WriteLine Anführungszeichen("</mso:customUI>")

'Zwischengespeicherte Datei schließen
tsZwischenspeicher.Close

Stream.Open
Stream.Type = 2 'text
Stream.Charset = "utf-8"
Stream.LoadFromFile strTempSpeicherpfad
FSO.OpenTextFile(strSpeicherpfad, 2, True, True).Write Stream.ReadText
Stream.Close

Outlook has to be restarted and the new Ribbon will be displayed.

0

Working in Outlook 2010, 14.0.7232.5000:

In ThisOutlookSession:

Private WithEvents Button As Office.CommandBarButton

Private Sub Application_Startup()
  Dim oExplorer As Outlook.Explorer
  Set oExplorer = Application.ActiveExplorer
  ' Dynamically create button at  Outlook startup (no need for XML file)
  Set Button = CreateCommandBarButton(oExplorer.CommandBars)
end sub



Private Sub Button_Click(ByVal Ctrl As Office.CommandBarButton, CancelDefault As Boolean)
  ' Code to be executed upon clicking the button.
  ' The name of the function MUST be "buttonname_Click", with "buttonname" 
  ' defined in Application_Startup().
  MsgBox "Click: " & Ctrl.Caption
End Sub



Private Function CreateCommandBarButton(oBars As Office.CommandBars) As Office.CommandBarButton
  On Error Resume Next
  Dim oMenu As Office.CommandBar
  Dim oBtn As Office.CommandBarButton
  Const BAR_NAME As String = "YourCommandBarName"
  Const CMD_NAME As String = "YourButtonName"

  Set oMenu = oBars(BAR_NAME)
  If oMenu Is Nothing Then
    Set oMenu = oBars.Add(BAR_NAME, msoBarTop, , True)
    Set oBtn = oMenu.Controls.Add(msoControlButton, , CMD_NAME, , True)
    oBtn.Caption = CMD_NAME
    oBtn.Tag = CMD_NAME

  Else
    Set oBtn = oMenu.FindControl(, , CMD_NAME)
    If oBtn Is Nothing Then
      Set oBtn = oMenu.Controls.Add(msoControlButton, , CMD_NAME, , True)
    End If
  End If

  oMenu.Visible = True
  Set CreateCommandBarButton = oBtn
End Function
jumpjack
  • 841
  • 1
  • 11
  • 17