Outlook (2007) Ribbon: Issue adding new group to Message tab - Code-Tips.com - Web Development, Programming, SEO

Thursday, January 27, 2011

Outlook (2007) Ribbon: Issue adding new group to Message tab

When writing a COM Addin for Microsoft Outlook 2007, I came across a scenario where I needed to add buttons to the Ribbon when composing or reading an email message.  Using the IRibbonExtensibility methods, I was able to add a new tab with groups and controls to the read and compose inspectors.  The issue was that I needed to include an additional group in the Message tab of an email when composing or viewing.  If I set the tab id to that of the read or compose message class (TabNewMailMessage or TabReadMessage), the group would not display at all.


The new group would only display if added to a new tab.  When I set the tab id to that of the Message tab for the read and compose message classes, the new group would not display at all.  This was not ideal, as the solution required that the new group and buttons were displayed first before the standard groups within the Message Tab.  


There were a number of reasons that the custom group would not display in the Message tab on the Ribbon:


1. I was using the id attribute instead of the idMso attribute for the tab.  To reference the built-in Message tab, I needed to use the idMso attribute (idMso="TabNewMailMessage" or idMso="TabReadMessage", not id="TabNewMailMessage").


2. Include startFromScratch attribute in Ribbon element (startFromScratch="true").  When I first included the startFromScratch attribute set to true, I was referencing a different tab to the Message tab, so the result was that all tabs were removed except for my new tab and groups contained.  After changing the id attribute to idMso and the value to the correct identifier for each Message class, my group was then added to the end of the built in Message tab.  I then moved the new group to the beginning of the tab using the insertBeforeMso attribute and the built-in message class tag ids.


The code used to add the new group to the Message tab is below.  I first configure the button element to be added to the group, but a different button is added depending on the Message Class.  The next step was to create the Custom Ribbon XML to include the other elements required to render the customisations, and add to a single string variable that includes the required button element.  

An alternative may be to read the entire XML file containing the new Ribbon elements from a file or application/addin resource.  As I wanted to use the same tab, but have a different button be added depending on the message class, I chose to generate the custom ribbon XML using VB below.




(Note: Some lines of code may be spanned across multiple lines in the post.  Copy to a text file to fix)
Public Function IRibbonExtensibility_GetCustomUI(ByVal RibbonID As String) As String

Dim strXMLPath As String
Dim strXMLOpen As String
Dim strXMLClose As String
Dim strXML As String
Dim strIn As String
Dim iFile As Integer
Dim strBtnXML As String
Dim strTabID As String
Dim strInsertBeforeGroupID As String

Select Case RibbonID
Case "Microsoft.Outlook.Mail.Compose"
' Return the RibbonX markup stored as a resource in the project
strBtnXML = "<button id=""VBBtn1"" size=""large"" label=""New Button 1"" onAction=""cmdPressed1"" getSupertip=""getSuperTip"" getImage=""getImageDisplay"" />" & vbCrLf
strTabID = "TabNewMailMessage"
strInsertBeforeGroupID = "GroupClipboard"

Case "Microsoft.Outlook.Mail.Read"
' Return the RibbonX markup stored as a resource in the project
strBtnXML = "<button id=""VBBtn2"" size=""large"" label=""New Button 2"" onAction=""cmdPressed2"" getSupertip=""getSuperTip"" getImage=""getImageDisplay"" />" & vbCrLf
strTabID = "TabReadMessage"
strInsertBeforeGroupID = "GroupShow"

Case Else
IRibbonExtensibility_GetCustomUI = ""
Exit Function
End Select

strXMLOpen = "<?xml version=""1.0"" encoding=""utf-8"" ?>" & vbCrLf
strXMLOpen = strXMLOpen & " <customUI xmlns = ""http://schemas.microsoft.com/office/2006/01/customui"" onLoad = ""Ribbon_OnLoad"" >" & vbCrLf
strXMLOpen = strXMLOpen & " <ribbon startFromScratch=""true"">" & vbCrLf
strXMLOpen = strXMLOpen & " <tabs>" & vbCrLf
strXMLOpen = strXMLOpen & " <tab idMso=""" & strTabID & """ label=""Message"" visible=""true"" >" & vbCrLf
strXMLOpen = strXMLOpen & " <group id=""GroupVBActions"" label=""" & ribbonGroupText & """ visible=""true"" insertBeforeMso=""" & strInsertBeforeGroupID & """ >" & vbCrLf

strXMLClose = " </group>" & vbCrLf
strXMLClose = strXMLClose & " </tab>" & vbCrLf
strXMLClose = strXMLClose & " </tabs>" & vbCrLf
strXMLClose = strXMLClose & " </ribbon>" & vbCrLf
strXMLClose = strXMLClose & "</customUI>" & vbCrLf


strXML = strXMLOpen & strBtnXML & strXMLClose

IRibbonExtensibility_GetCustomUI = strXML

End Function




The function stores XML data in separate strings.  It first determines which button is required based on the Message class (reading or composing a message), the adds the button XML to the strBtnXML string variable.


The remaining components of the XML are then added to the strXMLOpen and strXMLClose variables, and the complete XML constructed and stored in the strXML variable.  Storing the separate components of the XML is not a requirement, as it is only configured this way to allow the XML contents to be represented in code in a readable manner.


Resources:

0 comments:

Post a Comment