Add the code in Listing to the This Addin class replacing the autogenerated Startup and Shutdown event handlers

Listing 14.31. Code to read worksheet styles and populate the Custom task pane

Dim WithEvents worksheet As Excel.Worksheet

Dim myCustomTaskPane As Microsoft.Office.Tools.CustomTaskPane Dim myControl As UserControl1

Private Sub ThisAddIn_Startup(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles Me.Startup

' Start of VSTO-generated code

Me.Application = CType(Microsoft.Office.Tools.Excel _ .ExcelLocale1033Proxy.Wrap(GetType( _

Excel.Application), Me.Application), Excel.Application) ' End of VSTO-generated code worksheet = Globals.ThisAddIn.Application.ActiveSheet myControl = New UserControl1 Me.CustomTaskPanes.Add(myControl, _

"Styles and Formatting").Visible = True

End Sub

Private Sub ThisAddIn_Shutdown(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles Me.Shutdown

End Sub

Private Sub worksheet_SelectionChange(ByVal Target As _ Microsoft.Office.Interop.Excel.Range) Handles _ worksheet.SelectionChange

' Set a variable to the label on the actions pane control. Dim Actions As System.Windows.Forms.Label = _ myControl.SelectionFont

' Retrieve font information from currently selected cell. Dim FontName As String = Application.ActiveCell.Font.Name Dim FontSize As Short = CType( _

Application.ActiveCell.Font.Size, Short) Dim FontBold As Boolean = Application.ActiveCell.Font.Bold Dim FontItalic As Boolean = _

Application.ActiveCell.Font.Italic Dim FontAttribute As FontStyle = FontStyle.Regular Dim ExcelStyle As Excel.Style = Application.ActiveCell.Style Dim StyleName As String = ExcelStyle.Name

' Apply Bold, Italic, and Underline styles to label if they ' are applied to the active cell. If FontBold = True Then

FontAttribute = FontStyle.Bold End If

If FontItalic = True Then

FontAttribute = FontStyle.Italic End If

If Application.ActiveCell.Font.Underline = _

Excel.XlUnderlineStyle.xlUnderlineStyleSingle Then FontAttribute = FontStyle.Underline End If

' Add the Style name and formatting of the active cell to the ' label on the actions pane. Actions.Text = StyleName

Actions.Font = New Font(FontName, FontSize, FontAttribute) End Sub

0 0

Post a comment