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)
Adding Ribbon in your project:
Select Ribbon (Visual
Designer) item and add
Note: this project requires Visual Studio 2010 and
Word 2010 to run successfully.
Ribbon will be add in
this view.
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 designer control in Ribbon1.designer.vb file:
Friend WithEvents Tab1 As Microsoft.Office.Tools.Ribbon.RibbonTab
Friend WithEvents Group1 As Microsoft.Office.Tools.Ribbon.RibbonGroup
Friend WithEvents Button1 As Microsoft.Office.Tools.Ribbon.RibbonButton
Me.Tab1
= Me.Factory.CreateRibbonTab
Me.Group1
= Me.Factory.CreateRibbonGroup
Me.Button1
= Me.Factory.CreateRibbonButton
Me.Tab1.SuspendLayout()
Me.Group1.SuspendLayout()
'
'Tab1
'
Me.Tab1.ControlId.ControlIdType
= Microsoft.Office.Tools.Ribbon.RibbonControlIdType.Office
Me.Tab1.Groups.Add(Me.Group1)
Me.Tab1.Label
= "TabAddIns"
Me.Tab1.Name
= "Tab1"
'
'Group1
'
Me.Group1.Items.Add(Me.Button1)
Me.Group1.Label
= "Group1"
Me.Group1.Name
= "Group1"
'
'Button1
'
Me.Button1.Label
= "Insert
DateTime"
Me.Button1.Name
= "Button1"
'
'Ribbon1
'
Me.Name
= "Ribbon1"
Me.RibbonType
= "Microsoft.Word.Document"
Me.Tabs.Add(Me.Tab1)
Me.Tab1.ResumeLayout(False)
Me.Tab1.PerformLayout()
Me.Group1.ResumeLayout(False)
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
groups 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 AsMicrosoft.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