Option Strict On Option Explicit On Imports Microsoft.Office.Core imports Extensibility imports System.Runtime.InteropServices Imports System.IO Imports System.Collections Imports Microsoft.Win32 #Region " Read me for Add-in installation and setup information. " ' When run, the Add-in wizard prepared the registry for the Add-in. ' At a later time, if the Add-in becomes unavailable for reasons such as: ' 1) You moved this project to a computer other than which is was originally created on. ' 2) You chose 'Yes' when presented with a message asking if you wish to remove the Add-in. ' 3) Registry corruption. ' you will need to re-register the Add-in by building the StyleChooserSetup project ' by right clicking the project in the Solution Explorer, then choosing install. #End Region _ Public Class Connect Implements Extensibility.IDTExtensibility2, IRibbonExtensibility Dim WithEvents applicationObject As Microsoft.Office.Interop.Word.Application Dim addInInstance As Microsoft.Office.Core.COMAddIn Dim rib As IRibbonUI Dim documents As Hashtable = New Hashtable(10) Dim documents_styleNumbers As Hashtable = New Hashtable(10) Public Sub OnBeginShutdown(ByRef custom As System.Array) Implements Extensibility.IDTExtensibility2.OnBeginShutdown End Sub Public Sub OnAddInsUpdate(ByRef custom As System.Array) Implements Extensibility.IDTExtensibility2.OnAddInsUpdate End Sub Public Sub OnStartupComplete(ByRef custom As System.Array) Implements Extensibility.IDTExtensibility2.OnStartupComplete End Sub Public Sub OnDisconnection(ByVal RemoveMode As Extensibility.ext_DisconnectMode, ByRef custom As System.Array) Implements Extensibility.IDTExtensibility2.OnDisconnection End Sub Public Sub OnConnection(ByVal application As Object, ByVal connectMode As Extensibility.ext_ConnectMode, ByVal addInInst As Object, ByRef custom As System.Array) Implements Extensibility.IDTExtensibility2.OnConnection Dim version As String Dim nonRibbonApp As Boolean = False 'assign application and add-in instance objects applicationObject = DirectCast(application, Microsoft.Office.Interop.Word.Application) addInInstance = DirectCast(addInInst, Microsoft.Office.Core.COMAddIn) 'retrieve Word version version = applicationObject.Version 'account for any possible text in the Word version string that could fail the 'detection of the correct version. Get only the major version number (12) Dim blank As Char = " ".ToCharArray()(0) version = version.Split(" ".ToCharArray()(0))(0).Split(".".ToCharArray()(0))(0) 'check if the major version number is < 12. We need try-catch as this could 'fail if version is not a pure number Try If Convert.ToDouble(version) < 12 Then nonRibbonApp = True End If Catch nonRibbonApp = True End Try 'if we don't have Word 12 or later, unload the add-in If nonRibbonApp Then addInInstance.Connect = False End If End Sub Public Function GetCustomUI(ByVal RibbonID As String) As String Implements IRibbonExtensibility.GetCustomUI 'get the current assembly Dim assemb As System.Reflection.Assembly = System.Reflection.Assembly.GetExecutingAssembly() 'debug print all names of all embedded resources 'Dim t As String() = assemb.GetManifestResourceNames 'For Each i As String In t 'System.Diagnostics.Debug.WriteLine(i) 'Next i 'access the RibbonX.xml file Dim resource As Stream = assemb.GetManifestResourceStream("StyleChooser.RibbonX.xml") If Not resource Is Nothing Then 'read the file from the stream and make sure to close the stream Dim addInXml As StreamReader = New StreamReader(resource) Dim xml As String = addInXml.ReadToEnd() addInXml.Close() 'return the RibbonX code to Office Return xml End If 'if there is a problem with loading the resource file, return an empty XML file to avoid errors Return "" End Function 'callback for onAction Public Sub onAction(ByVal control As IRibbonControl) If control.Id = "aboutStyleChooser" Then System.Windows.Forms.MessageBox.Show("Microsoft Word 2007 Add-in - pschmid.net StyleChooser V1.2", "StyleChooser Add-In") End If End Sub Public Sub onPressed(ByVal control As IRibbonControl, ByVal pressed As Boolean) If control.Id = "checkBoxHideBuiltInStylesGroup" Then setStyleGroupVisibility(Not pressed) rib.Invalidate() End If End Sub 'return the number of items in comboBox Function GetItemCount(ByVal control As IRibbonControl) As Integer If control.Id = "comboStyleChooser" Then Dim document As Microsoft.Office.Interop.Word.Document = DirectCast(control.Context, Microsoft.Office.Interop.Word.Window).Document createStylesTable(document) Return DirectCast(documents(document), ArrayList).Count End If Return 0 End Function 'create a table of all styles a user can use for a particular document Public Sub createStylesTable(ByVal document As Microsoft.Office.Interop.Word.Document) Dim styles As ArrayList = New ArrayList(document.Styles.Count) Dim style As String Dim styleNumber As Integer = document.Styles.Count Dim rebuildTable As Boolean = False 'only rebuild the hash table if necessary If documents_styleNumbers.ContainsKey(document) Then If DirectCast(documents_styleNumbers(document), Integer) <> styleNumber Then rebuildTable = True End If Else rebuildTable = True End If If rebuildTable Then documents_styleNumbers(document) = styleNumber For Each i As Microsoft.Office.Interop.Word.Style In document.Styles If Not i.Hidden And i.InUse And _ (i.Type = Microsoft.Office.Interop.Word.WdStyleType.wdStyleTypeCharacter Or i.Type = Microsoft.Office.Interop.Word.WdStyleType.wdStyleTypeParagraph) Then styles.Add(formattedStyle(i)) End If System.Diagnostics.Debug.WriteLine(i.Description) ' System.Diagnostics.Debug.WriteLine(i.LinkStyle) Next i documents(document) = styles End If End Sub 'formats style with information whether it is a character, paragraph or linked paragraph style Private Function formattedStyle(ByVal style As Microsoft.Office.Interop.Word.Style) As String Dim formatStyle As String formatStyle = style.NameLocal.Trim() If style.Type = Microsoft.Office.Interop.Word.WdStyleType.wdStyleTypeCharacter Then formatStyle = "ª " + formatStyle ElseIf style.Type = Microsoft.Office.Interop.Word.WdStyleType.wdStyleTypeParagraph Then If style.Linked Then formatStyle = "¶ª " + formatStyle Else formatStyle = "¶ " + formatStyle End If End If Return formatStyle End Function 'return the label for one item in the comboBox Public Function GetItemLabel(ByVal control As IRibbonControl, ByVal index As Integer) As String If control.Id = "comboStyleChooser" Then Dim document As Microsoft.Office.Interop.Word.Document = DirectCast(control.Context, Microsoft.Office.Interop.Word.Window).Document Return DirectCast(DirectCast(documents(document), ArrayList)(index), String) End If Return "" End Function Public Sub onLoad(ByVal Ribbon As IRibbonUI) rib = Ribbon End Sub 'determines the currently active style Public Function GetText(ByVal control As IRibbonControl) As String If control.Id = "comboStyleChooser" Then Dim selection As Microsoft.Office.Interop.Word.Selection = DirectCast(applicationObject.Selection, Microsoft.Office.Interop.Word.Selection) Return formattedStyle(DirectCast(selection.Style, Microsoft.Office.Interop.Word.Style)) End If Return "" End Function 'change the style when user picks a different one in comboBox Public Sub onChange(ByVal control As IRibbonControl, ByVal text As String) If control.Id = "comboStyleChooser" Then Dim selection As Microsoft.Office.Interop.Word.Selection = DirectCast(applicationObject.Selection, Microsoft.Office.Interop.Word.Selection) selection.Style = text.Substring(3) End If End Sub 'return the visibilty passed on the registry parameter 'notice that when the Style group is visible, the QuickStlye Gallery and Change Style buttons in the add-in group are hidden Public Function getVisible(ByVal control As IRibbonControl) As Boolean If control.Id = "GroupQuickFormatting" Then Return isStyleGroupVisible() ElseIf control.Id = "GalleryChangeStyles" Then Return Not isStyleGroupVisible() ElseIf control.Id = "FlyoutAnchorChangeStyles" Then Return Not isStyleGroupVisible() End If End Function 'return the value for the checkbox. Notice that the checkbox logic is opposite of what is stored in the registry Public Function getPressed(ByVal control As IRibbonControl) As Boolean If control.Id = "checkBoxHideBuiltInStylesGroup" Then Return Not isStyleGroupVisible() End If End Function 'query registry to find out if MS style group is visible Private Function isStyleGroupVisible() As Boolean Dim rKey As RegistryKey = Microsoft.Win32.Registry.CurrentUser.CreateSubKey("Software\\pschmid.net\\StyleChooser") Dim keyValue As String = DirectCast(rKey.GetValue("StyleGroupVisible"), String) If keyValue Is Nothing Then keyValue = "" End If If keyValue <> "" Then Try Return Convert.ToBoolean(keyValue) Catch Return True End Try Else Return True End If End Function 'save style group visibility in registry Private Sub setStyleGroupVisibility(ByVal visible As Boolean) Dim rKey As RegistryKey = Microsoft.Win32.Registry.CurrentUser.CreateSubKey("Software\\pschmid.net\\StyleChooser") rKey.SetValue("StyleGroupVisible", visible) End Sub 'event to change currently shown style Private Sub applicationObject_WindowSelectionChange(ByVal Sel As Microsoft.Office.Interop.Word.Selection) Handles applicationObject.WindowSelectionChange rib.InvalidateControl("comboStyleChooser") End Sub End Class