How to add Ribbon in a Word


The most feature of office 2007 and later version is that these office systems are Ribbon compatible.Basically Ribbon in office system is the user interface element that contains controls. Or you can say ribbon is the parant control of command controls.
 
In this article we will learn that how to customize this ribbon in word application. If you want to see that how to start vsto project click here

We can customize ribbon with two ways in our word addin

Ribbon(visual Designer)
Ribbon(XML)

In this article we will discuss about Ribbon(Visual Designer) control.

Adding Ribbon in your project:

Select Ribbon (Visual Designer) item and add

You can see built in 'TabAddIns' tab that contains a group without any control. We can add various office ribbon controls from toolbox. You can also set the various properties of the ribbon, tab, group and contained controls from properties windows.

You can see Ribbon designer control in Ribbon1.designer.vb file:

  1.     Friend WithEvents Tab1 As Microsoft.Office.Tools.Ribbon.RibbonTab
  2.     Friend WithEvents Group1 As Microsoft.Office.Tools.Ribbon.RibbonGroup
  3.     Friend WithEvents Button1 As Microsoft.Office.Tools.Ribbon.RibbonButton
  4.  
  5.        Me.Tab1 = Me.Factory.CreateRibbonTab
  6.         Me.Group1 = Me.Factory.CreateRibbonGroup
  7.         Me.Button1 = Me.Factory.CreateRibbonButton
  8.         Me.Tab1.SuspendLayout()
  9.         Me.Group1.SuspendLayout()
  10.         '
  11.         'Tab1
  12.         '
  13.         Me.Tab1.ControlId.ControlIdType = Microsoft.Office.Tools.Ribbon.RibbonControlIdType.Office
  14.         Me.Tab1.Groups.Add(Me.Group1)
  15.         Me.Tab1.Label = "TabAddIns"
  16.         Me.Tab1.Name = "Tab1"
  17.         '
  18.         'Group1
  19.         '
  20.         Me.Group1.Items.Add(Me.Button1)
  21.         Me.Group1.Label = "Group1"
  22.         Me.Group1.Name = "Group1"
  23.         '
  24.         'Button1
  25.         '
  26.         Me.Button1.Label = "Insert DateTime"
  27.         Me.Button1.Name = "Button1"
  28.         '
  29.         'Ribbon1
  30.         '
  31.         Me.Name = "Ribbon1"
  32.         Me.RibbonType = "Microsoft.Word.Document"
  33.         Me.Tabs.Add(Me.Tab1)
  34.         Me.Tab1.ResumeLayout(False)
  35.         Me.Tab1.PerformLayout()
  36.         Me.Group1.ResumeLayout(False)
  37.         Me.Group1.PerformLayout()

Adding controls in Ribbon:

Follow these basic steps to design the Ribbon layout:

  1. Add a tab to the Ribbon.
  2. Add group to the tab.
  3. Add controls to the groups.

Controls can be dropped only on groups; you cannot drag a control directly to a tab or to the Ribbon. Groups can be dropped only on tabs; you cannot drag a group directly to a Ribbon.


Event handling:

Suppose you want to add a button on group for adding date time in the document on the click of button. You can drag drop button into the group from office ribbon Controls toolbox tab and you need to write code on its click event


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs) Handles Button1.Click
        Dim CurrentDate As String = DateTime.Now.ToShortDateString
        Dim rng As Word.Range = Globals.ThisAddIn.Application.Selection.Range
        rng.Text = CurrentDate
End Sub

Run this Example:

Build the project and Press F5.
(word document appeares The Ribbon of the document displays a custom tab.)

Requirements :

This example requires the following applications:
Visual Studio Tools for Office.
Microsoft Office word 2010.


Similar Articles