In Solution Explorer rightclick Ribbonvb and then click View Code Ribbonvb opens in code view in Visual Studio Expand the Ribbon Callbacks region as shown in Figure

* Ldt pi>Kt Offa ">■> " . >. >i i - tlft jij-j'jj i-j i _ - ■ t , i y j □

* Ldt pi>Kt Offa ">■> " . >. >i i - tlft jij-j'jj i-j i _ - ■ t , i y j □

<V* THrfddftub Ffc 5r«n* t X

ft i>I] 0

Implement» Off ice.ISibboaEsttensibilitr

PC Ivrit-r = aUjun to Of f ice. TP LbbcmUI

, J] TOiAMln.*, Ji MrfVutKt j] Rttcrl.nW W 13 -.>*r(teftddrijet«S

+ JhtKttdlMtnhro»

1 Public Sub Mewtt End Li "Ob

Public function «3«lCuSte»Ui (ByVal iIMjuqID A3 aviltiy) JLs ati in« Implements CUtlfe. iftltitionEiilent

Peturri CecfieaaurceTeKt ("SanipleAdam. P:ifafaonl.xiil,T) Cntl function

:. ED

Public Sub 0nU**t(8vv<.) ttWK>nin to Offk«r.uubbonOT)

He. r lKbon ■ rlbbonOf End 3ub

Public 3«4> OoT<KHfle8«ittcsl(ByVnl conteol to Oiiice-TPlbbonContsolj Byv*] to Boolean* It IsPresKd Tfien newweto* - < "PreMed"]

Hessa je Bon. Show < "Pelewcd"] ma it End. Sub ttzoa Hegign


Figure 14.11. Looking at Ribbon1.vb

The Ribbonl.vb code file has a GetCustomUI method, which is required in classes that implement IRibbonExtensibility. The Ribbon Callbacks region contains an OnToggleButtonl method that checks the state of the toggle button (on or off) and displays text accordingly. You can write code to handle the events of the buttons you add to the Ribbon in this region.

Next, open the XML file to see the XML that was generated for the Ribbon. This XML markup is what tells the application which additional customizations to display on the Ribbon. In Solution Explorer, right-click Ribbonl.XML and then click Open. The Ribbonl.xml file opens in code view in Visual Studio, as shown in Figure 14.12.


id it View Project Build Debug XML Data Tools Window Community Help


B'Cf B0|* IS | *> • • # • ^ 1 ► M«g

t Any CPU ^

• l^a1

us a ma- j



» a £ S. ppt»«u


'Ribbonl.xml Ribbonl.vb ' Thjs^ddln.vb f- RteS^terc IE*cslAddInlSetup)

Start Paoe - .X

qkcustomUI xmlns="

..^ .' i n t'l/ Lttr 1" 1 < ly-.ii*"i>r. r.v.vrJ'rs. ft

Sift S3


3 <tabs>

J1 Solution 'Exce EKcelfldo



3 <tab itaHsD="TatiA(ldIns,r>


<group id="HyGroup" 3 latoel="Hy Group">

<toggleButton id= "toggleButtonl"

label="My Button"



scceentip="Hy Button Scceentip"


imageMso="HappyFace" />

</group> </taJi> </tafcis>


L < / c us t omUI>

Figure 14.12. Looking at Ribbonl.xml

Figure 14.12. Looking at Ribbonl.xml

In the first node under customUI, you'll see the <ribbon> node. This represents the area on which the tabs and controls reside. The next node, <tabs>, contains all the <tab> nodes that you are adding to the Ribbon. In this case, VSTO 2005 SE uses the built-in Add-Ins tab. The next node is the <group> node. You can have multiple groups on a tab, but by default VSTO 2005 SE adds a single group that is labeled "My Group." Any buttons that are to appear in the group would be listed as child nodes of <group>. VSTO 2005 SE adds a single toggle button labeled "My Button" to MyGroup. Notice that the onAction attribute is set to OnToggleButton1, the name of the method that exists in the Ribbon1.vb file. This method is called each time the end user clicks the toggle button.

The idMso qualifier is for built-in tabs, groups, and controls. You use the id qualifier for custom tabs, groups, and controls that you create. You can use the idQ qualifier when you want to share the same custom group in two different add-ins.

It is easy to modify the Ribbon. In this section you will change the label of the built-in Add-Ins tab to "Format Tab," change the group label to "Format Group," and add three other toggleButton controls to the group to display default Bold, Italic, and Underline buttons. To do this, change the XML markup in Ribbon1.xml as shown in Listing 14.2.

Listing 14.2. XML markup in the Ribbonl.xmlfile

<customUI xmlns=

"" onLoad="OnLoad"> <ribbon> <tabs>

<tab idMso="TabAddIns" label="Format Tab"> <group id="MyGroup"

label="Format Group"> <toggleButton id="toggleButton1" size="large" label="My Button" screentip="My Button Screentip" onAction="OnToggleButton1" imageMso="HappyFace" /> <toggleButton idMso="Bold" /> <toggleButton idMso="Italic" /> <toggleButton idMso="Underline" /> </group> </tab> </tabs> </ribbon> </customUI>

When you press F5 to run the code, Excel displays a group labeled "Format Group" in a tab labeled "Format Tab." The group contains four toggle buttons, as shown in Figure 14.13.

Bookl- Microsoft Ex~c'e I

Home Insert Page Layout Formulas Data Review View | Format Tab

My Button H

Format Group

My Button H

Format Group














Figure 14.13. Customizing the Ribbon

0 0

Post a comment