Power BI

Content by Charles Sterling

Creating a Ribbon for Office 2007

If you are are playing with Andrew’s VSTO Tutorial  and are having with the image rendering you might have put your GetResourceBitmap function into your ImageConverterclass like i was doing issues (Thanks Kathleen McGrath for finding my mistake!!) i have pasted the completed code below


#Region “Usinng directives”

Imports System
Imports System.Collections.Generic
Imports System.Diagnostics
Imports System.Text
Imports System.Reflection
Imports System.Runtime.InteropServices
Imports System.Windows.Forms
Imports Office = Microsoft.Office.Core

#End Region

#Region “service request implementation”

Partial Public Class ThisApplication

 Protected Overrides Function RequestRibbonService() As Object
  Return New Ribbon1()
 End Function

End Class

#End Region

<ComVisible(True)> _
 Public Class Ribbon1
 Implements Office.IRibbonExtensibility

#Region “Fields”
 Private ribbon As Office.IRibbonUI
#End Region

#Region “Initialization”

 Public Sub New()
 End Sub

 Public Function GetCustomUI(ByVal ribbonID As String) As String Implements Office.IRibbonExtensibility.GetCustomUI
  Return GetResourceText(“Ribbon1.xml”)
 End Function

 Public Sub OnLoad(ByVal ribbonUI As Office.IRibbonUI)
  Me.ribbon = ribbonUI
 End Sub

#End Region

 Public Sub OnToggleButton(ByVal control As Office.IRibbonControl, ByVal isPressed As Boolean)
  If (isPressed) Then
   MessageBox.Show(“Hello World!”)
  End If

    End Sub

    Public Shared Function GetResourceBitmap( _
    ByVal resourceName As String) As System.Drawing.Bitmap
        Dim asm As Assembly
        Dim resources As String()
        Dim image As System.Drawing.Bitmap

        asm = Assembly.GetExecutingAssembly()
        resources = asm.GetManifestResourceNames()
        image = Nothing

        For Each resource As String In resources
            If (resource.EndsWith(resourceName)) Then
                Dim stream As System.IO.Stream
                stream = asm.GetManifestResourceStream(resource)
                If (stream Is Nothing) Then
                    Exit For
                End If

                Dim extension As String
                extension = System.IO.Path.GetExtension(resourceName).ToLower()
                Select Case extension
                    Case “.ico”
                        image = New System.Drawing.Icon(stream).ToBitmap()
                    Case “.jpg”
                    Case “.bmp”
                    Case Else
                        image = New System.Drawing.Bitmap(stream)
                End Select

                Exit For
            End If
        Return image
    End Function

    Public Function GetImage(ByVal control As Office.IRibbonControl) As stdole.IPictureDisp
        Dim pictureDisp As stdole.IPictureDisp
        pictureDisp = Nothing

        Select Case control.Id
            Case “toggleButton”
                pictureDisp = ImageConverter.Convert(GetResourceBitmap(“blankcd.ico”))
        End Select
        Return pictureDisp
    End Function

#Region “Helpers”
 Public Shared Function GetResourceText(ByVal resourceName As String) As String
  Dim asm As Assembly = Assembly.GetExecutingAssembly()
  Dim resources As String() = asm.GetManifestResourceNames()
  For Each resource As String In resources
   If resource.EndsWith(resourceName) Then
    Dim resourceReader As System.IO.StreamReader
    resourceReader = New System.IO.StreamReader(asm.GetManifestResourceStream(resource))
    If Not resourceReader Is Nothing Then
     Return resourceReader.ReadToEnd()
    End If
   End If
  Return Nothing
 End Function
#End Region
End Class

Friend Class ImageConverter
    Inherits System.Windows.Forms.AxHost

    Sub New()
    End Sub

    Public Shared Function Convert(ByVal image As System.Drawing.Image) As stdole.IPictureDisp
        Return AxHost.GetIPictureDispFromPicture(image)
    End Function

End Class




<customUI xmlns=”http://schemas.microsoft.com/office/2006/01/customui” onLoad=”OnLoad”>
  <ribbon startFromScratch=”false”>
      <tab id=”VSTO.Tab” label=”Hello From Chuck” visible=”1″>
        <group id=”VSTO.Group” label=”Hello Kathleen” visible=”1″>
          <toggleButton id=”toggleButton” label=”Icon Here Now”
               screentip=”Hello World Tooltip” onAction=”OnToggleButton”
               size=”large” getImage=”GetImage”/>