How To Create Custom Ribbon In Microsoft Word Using VSTO Add-in

This article demonstrates how we can utilize VSTO add-in to create custom ribbon in word, how to add custom buttons/actions in word and what can be done with these custom actions. The custom ribbons can help to extend Microsoft Word to perform your own actions, like validating the documents, inserting template texts in contents, etc.

Prerequisites

We need the following setup to perform below steps.

Let’s get started with steps now on how to do this.

1. Create a project in Visual Studio. Select “Word VSTO Add-in”, because we are creating add-in for word. Select Next & name it as CustomRibbon in next window and Save.

2. A new project will be created with ThisAddin.cs class out of the box.

3. Go to Solution Explorer, right click on project CustomRibbon and add new item, select Ribbon (XML), change name to CustomActionsRibbon.cs. Click on Add

CustomActionRibbon.cs is added to project along with an XML file named CustomActionsRibbon.xml

4. Right-click on ThisAddin.cs and click on View Code.

Add following code to ThisAddin.cs.

protected override Microsoft.Office.Core.IRibbonExtensibility CreateRibbonExtensibilityObject()
{
    return new CustomActionsRibbon();
}

This overrides the CreateRibbonExtensibilityObject method and returns the ribbon xml class to the Office application.

Build the project to make sure no build errors.

5. From solution explorer, open CustomActionsRibbon.xml.

Copy paste below contents in XML

<?xml version="1.0" encoding="UTF-8"?>
<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui" onLoad="Ribbon_Load">
	<ribbon>
		<tabs>
			<tab idMso="TabAddIns">
				<group id="ContentGroup" label="Content">
					<button id="textButton" label="Insert Content"
						 screentip="Text" onAction="OnTextButton"
						 supertip="Inserts text content at the cursor location."/>
					<button id="msgButton" label="Show Message"
						 screentip="Message Box" onAction="OnMsgButton"
						 supertip="Shows Message Box."/>
				</group>
			</tab>
		</tabs>
	</ribbon>
</customUI>

First Ribbon button, inserts text at the cursor location.

Second ribbon button, shows popup message box.

6. Now as we have added button and actions in step 5, it is time to implement logic for the above action buttons.

For that, we need to implement onAction callback methods in our CustomActionsRibbon.cs class.

Copy below code in CustomActionsRibbon.cs class

public void OnTextButton(Office.IRibbonControl control)
{
    Microsoft.Office.Interop.Word.Range currentRange = Globals.ThisAddIn.Application.Selection.Range;
    currentRange.Text = "Content from custom ribbon.";
}
public void OnMsgButton(Office.IRibbonControl control)
{
    MessageBox.Show("This is popup from custom ribbon" + Environment.NewLine + "Hello..", "Popup");
}

7. After running the code

Add-ins tab is showing two buttons

Clicking on first button (Insert Content)

Clicking on second button (Show Message)

That's it... this article was to explain the basic on how we can start with custom ribbons, there are many other things we can do on top of above setup. In my next article, we will see how we can extend this even further and add validations in word documents.


Similar Articles