Listing 14.1. Adding code to the Startup event handler of a PowerPoint add-in
Private Sub ThisAddIn_Startup(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.Startup
MsgBox(Me.Application.Name & " " & _
Me.Application.Version)
End Sub
-------------------------------------------------------------------------------
Listing 14.2. XML markup in the Ribbon1.xml file
<customUI xmlns=
"http://schemas.microsoft.com/office/2006/01/customui"
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>
-------------------------------------------------------------------------------
Listing 14.3. Adding XML markup to Ribbon1.xml
<customUI xmlns=
"http://schemas.microsoft.com/office/2006/01/customui"
onLoad="OnLoad">
<ribbon>
<tabs>
<tab idMso="TabHome">
<group id="NewFont" insertAfterMso="GroupFont"
label="More Fonts">
<toggleButton idMso="TextAllCaps" />
<toggleButton idMso="UnderlineDouble" />
</group>
</tab>
</tabs>
</ribbon>
</customUI>
-------------------------------------------------------------------------------
Listing 14.4. Adding descriptions to controls
<toggleButton idMso="TextAllCaps" supertip="Make the selected
text all caps." />
<toggleButton idMso="UnderlineDouble" label="Dbl Underline"
supertip="Double underline the selected text."/>
-------------------------------------------------------------------------------
Listing 14.5. Adding the BitmapToIPicture class
Public Class BitmapToIPicture
Inherits System.Windows.Forms.AxHost
Public Sub New()
MyBase.New(Nothing)
End Sub
Public Shared Function Convert(ByVal _
Image As System.Drawing.Image) As stdole.IPictureDisp
Convert = GetIPictureFromPicture(Image)
End Function
End Class
-------------------------------------------------------------------------------
Listing 14.6. Adding the GetImage function
Public Function GetImage(ByVal control _
As Office.IRibbonControl) As stdole.IPictureDisp
Return BitmapToIPicture.Convert(My.Resources.BoldItalic)
End Function
-------------------------------------------------------------------------------
Listing 14.7. Adding code to handle the toggle button Click event
Public Sub ToggleBoldItalic(ByVal control As _
Office.IRibbonControl, ByVal isPressed As Boolean)
If (isPressed) Then
Globals.ThisAddIn.Application.Selection.Font.Bold = True
Globals.ThisAddIn.Application.Selection.Font.Italic _
= True
Else
Globals.ThisAddIn.Application.Selection.Font.Bold = False
Globals.ThisAddIn.Application.Selection.Font.Italic _
= False
End If
End Sub
-------------------------------------------------------------------------------
Listing 14.8. Set the getImage attribute to the GetImage method
<customUI xmlns=
"http://schemas.microsoft.com/office/2006/01/customui"
onLoad="OnLoad">
<ribbon>
<tabs>
<tab idMso="TabHome">
<group id="NewFont" insertAfterMso="GroupFont"
label="More Fonts">
<toggleButton idMso="TextAllCaps" supertip="Make the
selected text all caps." />
<toggleButton idMso="UnderlineDouble"
label="Dbl Underline"
supertip="Double underline the selected text."/>
<toggleButton id="BoldItalic"
getImage="GetImage"
size="normal"
label="Bold/Italic"
screentip="Bold and Italic"
onAction="ToggleBoldItalic"
supertip="Make the select text bold and italic." />
</group>
</tab>
</tabs>
</ribbon>
</customUI>
-------------------------------------------------------------------------------
<button id="myButton" label="Printer Setup"
imageMso="FilePrint" screentip="set up the printer"
onAction="OnClick" />
Listing 14.9. Adding a Button control
Public Sub OnClick(ByVal control As Office.IRibbonControl)
Globals.ThisAddin.Application.Dialogs( _
Excel.XlBuiltInDialog.xlDialogPrinterSetup).Show()
End Sub
-------------------------------------------------------------------------------
<checkBox id="hideWorksheet" label ="Hide Sheet1"
onAction="OnCheck"/>
Listing 14.10. Adding a check box control
Public Sub OnCheck(ByVal control As Office.IRibbonControl, _
ByVal isChecked As Boolean)
Dim Sheet1 As Excel.Worksheet = _
Globals.ThisAddin.Application.Sheets(1)
If isChecked Then
Sheet1.Visible = Excel.XlSheetVisibility.xlSheetHidden
Else
Sheet1.Visible = Excel.XlSheetVisibility.xlSheetVisible
End If
End Sub
-------------------------------------------------------------------------------
<comboBox id="MyCombo" label="Attributes" onChange="OnChange" >
<item id="Super" label="Superscript"/>
<item id="Sub" label="Subscript"/>
<item id="None" label="None"/>
</comboBox>
Listing 14.11. Adding a combo box control
Public Sub OnChange(ByVal control As Office.IRibbonControl, _
ByVal text As String)
Dim CurrentRange As Excel.Range = Globals.ThisAddin _
.Application.Selection
Select Case text
Case "Subscript"
CurrentRange.Font.Subscript = True
Case "Superscript"
CurrentRange.Font.Superscript = True
Case "None"
CurrentRange.Font.Subscript = False
CurrentRange.Font.Superscript = False
End Select
End Sub
-------------------------------------------------------------------------------
Listing 14.12. XML markup for adding a gallery to the Ribbon
<gallery id="galleryPrinter" label="Printer" size="large"
columns="2" imageMso="FilePrint" onAction="OnAction"
itemWidth="25" itemHeight="25" >
<item id="Print" imageMso="FilePrint" label="Print"/>
<item id="PrintPreview" imageMso="FilePrintPreview"
label="Print Preview"/>
<item id="QuickPrint" imageMso="FilePrintQuick"
label="Quick Print"/>
<item id="PrintSetup" imageMso="PrintSetupDialog"
label="Print Setup"/>
<item id="PrintOptions" imageMso="PrintOptionsMenuWord"
label="Print Options"/>
</gallery>
-------------------------------------------------------------------------------
Listing 14.13. Handling the OnAction event of a gallery on the Ribbon
Public Sub OnAction(ByVal control As Office.IRibbonControl, _
ByVal controlID As String, ByVal index As Integer)
Select Case controlID
Case "Print"
Globals.ThisAddin.Application.Dialogs( _
Word.WdWordDialog.wdDialogFilePrint).Show()
Case "PrintPreview"
Globals.ThisAddin.Application.ActiveDocument. _
PrintPreview()
Case "QuickPrint"
Globals.ThisAddin.Application.PrintOut()
Case "PrintSetup"
Globals.ThisAddin.Application.Dialogs( _
Word.WdWordDialog.wdDialogFilePrintSetup).Show()
Case "PrintOptions"