Friday, November 12, 2010

Create your own Ribbon tab in Office Word

Ribbon interface is the command bar that organizes a program's features into a series of tabs at the top of a window. In this post we will see how to create a Office add-in project in Visual Studio and create your own custom tab  in the ribbon of Microsoft Word. Actually, by following the same routine you can create your own tabs in Excel and PowerPoint as well.

First, to use the ribbon you must create a “Word 2007 Add-in” project in Visual studio. Go to File –> New –> Project.. and the “New Project” dialog box will appear. choose the “Office” from the project types and select “2007” which is under that category. From the templates, choose “Word 2007 Add-in”. As I said, you can select either Excel 2007 Add-in or PowerPoint 2007 Add-in instead of the word add-in, then you can create tabs for those applications respectively.

Now you have the basic things for a word add-in. Then you have to add a ribbon to your project. Go to solution explorer and right-click on the project name. here it’s “MyWordAddin”. By right clicking you will get a menu and goto “Add” –> “New Item..” and select “Ribbon (Visual Designer)”. It allows you to add a ribbon to your project and then to customize it and enhance it by using available user controls. There is another one “Ribbon (XML)” which allows you to go more in depth customizations and design a ribbon from  the scratch using XML. But as the first step ‘Ribbon (Visual Designer)’ is easy and much more appropriate.

Now you have a ribbon awaiting for customization :) As the initial step, we will make a “Hello world” application that will just say “Hello world” after a button click. Add a button to the “group1” from the “Office Ribbon Controls”. Rename it as you like; here I’m setting its’ label as “Say Hello

Then the cording part. We will just add a message box to as “Hello World !” as the purpose is to just demonstrate the concept. (To use the message box, you have to use the System.Windows.Forms) I am not going to describe it that much as adding a message box is not a deal. Just write the code for button1 event handler like this.

  private void button1_Click(object sender, RibbonControlEventArgs e)
      MessageBox.Show("Hello World !", "Hello", MessageBoxButtons.OK);

Then build the project and run it. You must see the Office Word application starts automatically and in the ribbon you must see your own tab which you created a moment ago.

As the next step, we will see how to create an “Open File..” button. For that, first add an “Open File Dialog” to your ribbon from ‘Dialogs’ group in tool box. Then add another button to the ribbon (let’s say it’s “button2”. I have set it’s label as the “Open File”). To select only the Word documents, you must insert  the text; “Word 2007 Document|*.docx|Word 97-2003 Document|*.doc|All Files|*.*” for the Filter property of the openFileDialog1.

Then paste this code in the ThisAddIn.cs file.

  public static string opendFile;

  public static void openDoc()
      Word.ApplicationClass WordApp = new Word.ApplicationClass();
      object fileName = opendFile;
      object readOnly = false;
      object isVisible = true;

      // Here is the way to handle parameters you don't care about in .NET
      object missing = System.Reflection.Missing.Value;

      WordApp.Visible = true;

      // Open the document that was chosen by the dialog
      Word.Document aDoc = WordApp.Documents.Open(ref fileName, ref missing, ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref isVisible, ref missing, ref missing, ref missing, ref missing);
      // Activate the document so it shows up in front

And this code must be included as the event handler of the button2.

private void button2_Click(object sender, RibbonControlEventArgs e)

    ThisAddIn.opendFile = openFileDialog1.FileName;

     if (ThisAddIn.opendFile != ""|File.Exists(ThisAddIn.opendFile)){

Run your project and now you must have the facility to browse and open a word file by clicking the “'Open File” button. So that’s the basics about creating a ribbon tab and adding some user controls to it. You can create your own features in Word, Excel and PowerPoint in this way.

1 comment:

  1. Greetings Kalana,
    I am creating a Word AddIn for 2010. I want to use it for specific template. How can I do program in c# to find which template is loading? Please help in this issue.


Had to enable word verification due to number of spam comments received. Sorry for the inconvenience caused.