Creating a shared ribbon for Office 2013: Word, Excel and PowerPoint
It is a universal truth that sharing is a good thing. If we didn’t share, we wouldn’t have any friends. Instead of enjoying a pint with our buddies, we would live out our days locked inside four walls to write software with a few fluorescent lights and an aging computer. Wait! What?
Yes, sharing is good. Good for gaining friends and good when building Office solutions. Sharing makes efficient use of assets/resources and lets us to (sometimes) escape to go for lovely dinner with our buds… or our significant other.
In this article, I want to show you how you can create a single Microsoft Office 2013 and 2010 ribbon and share it with Excel, PowerPoint, and Word. This scenario is one of many where Add-in Express shines. Using the Add-in Express framework, you can easily build a shared ribbon in a matter of minutes. Best of all, you can also quickly configure your controls to display (or not to display) in the Office host applications your add-in targets.
Please take note that I said configure… not code. This is largely a point and click exercise. By the end of this article you’ll better understand why we say Add-in Express tools provide a true RAD experience.
Creating the add-in project for Office 2007-2013
This add-in contains a single Ribbon that will display in Microsoft Office 2007, 2010 and 2013 Excel, PowerPoint and Word. There are three ribbon button groups. Within each group resides a single button as you can see:
Each application has a corresponding group and button. Hopefully you have guessed that the trick here is to display only the controls residing in the group matching the host app: Excel, PowerPoint, or Word. And we are going to use Add-in Express for Office and .net.
Well, it’s not a trick at all as you will soon discover. Open Visual Studio and create a new ADX COM Add-in project. Select VB.Net (or C#, or C+++) as the language and…
Choose Excel, PowerPoint, and Word as the supported applications then click next until the Wizard closes and Visual Studio creates the project.
Now, let’s build our shared Office ribbon.
Building the shared Office ribbon
Building the ribbon will take just a few minutes. It’s as easy as completing these steps:
- Open the AddinModule in design view if isn’t already done for you.
- Add an AdxRibbonTab component to the AddinModule’s design surface.
- Select the tab and change its caption to ‘Shared Ribbon’.
- The tab’s Ribbon property should already be set to ExcelWorkbook;WordDocument;PowerPointPresentation but go ahead and confirm. This setting is what makes it a shared ribbon.
- Select the AdxRibbonTab to display its visual designer in the lower half of the AddinModule.
- Add three AdxRibbonGroup controls to the ribbon (just click the AdxRibbonGroup button in the visual designer’s toolbar). Change their captions to Excel, PowerPoint, and Word respectively. Also, set their Ribbons property to ExcelWorkbook, PowerPointPresentation, & WordDocument. The value of the Ribbons property causes the showing and hiding of the controls in the correct host apps.
- Now, add an AdxRibbonButton control to each group. Change each button’s properties according to this table:
After completing these steps, your ribbon should look like this:
Adding the code
We need to add code to each button’s click event. The code is just for illustrative purposes. What I want you to take away here is that in just a few minutes, you can have built a shared ribbon for Office 2007, 2010 and 2013 and implemented your business logic.
The Excel button (btnExcel) inserts a simple formula into the active cell.
Private Sub btnExcel_OnClick(sender As Object, _ control As IRibbonControl, pressed As Boolean) _ Handles btnExcel.OnClick Dim ActiveCell As Excel.Range = Me.ExcelApp.ActiveCell ActiveCell.Value2 = "=2+2" Marshal.ReleaseComObject(ActiveCell) End Sub
The PowerPoint button (btnPowerPoint) changes the theme of the active presentation and allows the user to toggle through various themes included with Office. Be careful with the hard coded themePath value. This path might be different on your system… especially if you are not running Office 2013.
Dim iCount = 1 Private Sub btnPowerPoint_OnClick(sender As Object, _ control As IRibbonControl, pressed As Boolean) _ Handles btnPowerPoint.OnClick ' Modify this to match your own environment: Const themePath As String = _ "C:\Program Files (x86)\Microsoft Office\Document Themes 15\" Const themeName1 As String = themePath & "Facet.thmx" Const themeName2 As String = themePath & "Integral.thmx" Const themeName3 As String = themePath & "Ion.thmx" Const themeName4 As String = themePath & "Organic.thmx" Const themeName5 As String = themePath & "Organic.thmx" Dim ActivePresentation As PowerPoint.Presentation = _ Me.PowerPointApp.ActivePresentation Select Case iCount Case 1 ActivePresentation.ApplyTheme(themeName1) iCount = iCount + 1 Case 2 ActivePresentation.ApplyTheme(themeName2) iCount = iCount + 1 Case 3 ActivePresentation.ApplyTheme(themeName3) iCount = iCount + 1 Case 4 ActivePresentation.ApplyTheme(themeName4) iCount = iCount + 1 Case 5 ActivePresentation.ApplyTheme(themeName5) iCount = iCount + 1 Case Else End Select Marshal.ReleaseComObject(ActivePresentation) End Sub
The Word button (btnWord) reprises the great logic provided by the Excel button. It’s deja vu all over again… but this time, it’s in Word!
Private Sub btnWord_OnClick(sender As Object, _ control As IRibbonControl, pressed As Boolean) _ Handles btnWord.OnClick Dim Selection As Word.Selection = Me.WordApp.Selection With Selection .InsertFormula(Formula:="=(2+2)/10", NumberFormat:="") .TypeParagraph() .Calculate() End With Marshal.ReleaseComObject(Selection) End Sub
We’re done! That’s it takes. Now click Build > Register ADX Project to build and register the add-in on your system. Now you can open Excel, PowerPoint, and Word to see the fruits of your labor…
The shared ribbon in in Excel 2013:
The shared ribbon in PowerPoint 2013:
The shared ribbon in Word 2013:
Please note that I disabled some tabs using the Customize Ribbon feature. I did it for the “sake of screenshot”! I wanted it to be simple and clean. It’s like photography to a developer/writer.
I need to get out more don’t I? Well I will. Now that I shared this handy technique with you… my friend.
This sample add-in was developed using Add-in Express for Office and .net: