Word StyleChooser - Optimization & VB.NET
Quick Links
-
Download StyleChooser V1.2.2 (requires .NET Framework 1.1 or higher). Unzip and click on “StyleChooserSetup.msi”
If you hoped that my last post would have been step-by-step explained how to create a COM Add-In with Visual Studio, then you should consult Microsoft’s Frank Rice’s post of the same day. Ironically, he posted a tutorial on how to create a managed C# COM-Add-In as well. He doesn’t seem to like any comments advertising my blog though, as all of my comments to that effect have been deleted so far.
As promised, I am providing the VB.NET version of the StyleChooser add-in today. Let me first though share with you feedback I received on the first version:
-
There were several setup-related issues. My VS setup project didn’t provide very meaningful information about itself to the user nor did it install itself in the expected folder. Version 1.1 that I quickly posted fixed those issues. (from Bob Buckland)
-
The location of the add-in on the Home tab wasn’t the most useful one. I moved it with V1.1 to the right of the Styles group. (from Bob Buckland)
-
“One of the key advantages of the old dialog was that you could run the width out to full screen so that when you had a style of Normal+Blue+13pt+Tahmoa+Bold you saw all of that when you turned on the option to track formatting. There’s no room for that width in the ribbon, but a tooltip, perhaps?” (from Bob Buckland)
-
“One thing I always disliked about the Styles dropdown is that it didn’t show the type of style. How can users be expected to apply proper styles if they don’t even know if it is a paragraph style or a character style? So maybe it would be possible to add ¶ for paragraph styles, ª for character styles, and ¶ª for those new linked styles?” (from Klaus Linke)
-
“Maybe it could be made to reflect the styles chosen to be displayed in the styles pane… If Word is used properly, that should only show a handful of styles.” (from Klaus Linke)
-
“Any way to assign Ctrl+Shift+S to it?” That shortcut opens the drop-down in Word 2003. (from Herb Tyson)
-
The users that complained about the inadequacy of the style choosing UI in Word 2007 (all of them Word or other MVPs by the way) probably could do without that huge Style group on the Home tab. Therefore it might be helpful to have an option to completely hide the MS Style group.
Conversion to VB.NET
Someone commented to me that VB developers generally don’t read C# examples, whereas C# developers have no problem reading and using VB.NET examples, I’ll switch this add-in to VB.NET only. It’s easier for me as well to maintain one code base only. For C#, you can download the V1.1 source code.
VB.NET can cast objects automatically for you. However, as I am firmly implanted nowadays in the C++/C#/Java world, I am very distrustful of automatic casting. Therefore, my VB.NET code uses “Option Strict On”.
The only surprise I had in converting the code was that VB.NET is not case-sensitive for variable names, while C# is. Took me a bit to remember that and fix the code appropriately. While testing the add-in, I discovered that if you change the style with the Styles Group and don’t move the cursor after that, the text field of the combo box displays the old style still. I looked through the Word Object Model, but couldn’t find any event that would be triggered if the style is changed. Unless someone points me to the correct event, the add-in will always have this issue.
You can download V1.1 of the VB.NET source code.
Changes
Showing the modifications to the current style requires a lot more effort. It seems that the formatting of the selection has to be compared to the formatting of the style and then the differences shown. I don’t see any easier way of doing that. That is a lot of work and therefore I won’t do it this time around.
Adding the information whether a style is a character, paragraph or linked paragraph style is rather straightforward. The information is nicely encoded in the Object Model, so it is only a question of formatting the string returned to Word correctly.
As I mentioned in my last post, the performance of the drop-down list is rather bad. The key performance issue is that the Hashtable containing all styles is created from scratch every time after the control was invalidated (which is after any cursor move, selection change). In order to speed this up, I implemented an optimization strategy. In this new version, the Hashtable is only rebuilt if the number of all styles reported by Word changed. That means, unless a style was removed or added, the Hashtable won’t be rebuilt. The downside is that if the user adds and removes a style without triggering any selection change in between the two operations, the drop-down list won’t reflect this change. Similarly, the drop-down list won’t catch any renaming of styles. The performance gain is so dramatic though that these two handicaps are acceptable.
The drop-down list should reflect the styles that are currently In-Use. However this doesn’t seem to work and might point to a bug in Word 2007 B2.
I still need to look into the shortcut issue.
Replacing the built-in Styles group
Why would a user still need the Styles group, if he or she has this particular add-in installed? For one thing, the user might like to still be able to access the Quick Style gallery and use the functionality provided by the Change Styles menu. That means, if the add-in was to replace the entire Styles group, those two items have to be replicated in order for the user to keep all functionality.
If you read my UI Style Guide post, you should be screaming “NOOO” at this point. I was very explicit about not hiding MS groups and controls, and especially not replacing them. I’ll weaken my own statement at this point a bit. In this case, I can actually replace the Styles group because I expect the users of this add-in to be only power users who know what they are doing. In addition, and even more importantly, the add-in will very visibly tell the user that the MS group is being hidden, and allow the user to change that. Also, the default behavior will be to NOT hide the MS group. This is one of those situations where I will violate the MS-group and the dynamic alteration of the ribbon rule, because anything else is a real waste of ribbon space. I’d love to hear your comments on how this add-in addresses both issues.

The first change in the RibbonX is that the Styles Group is now included with a “getVisible” callback. This callback will either return true or false, depending on whether the group should be shown or not.
The second change is that I introduced a check box to let the user pick whether to show or hide the MS Styles group. The “getPressed” callback tells Office whether it is checked or not while the “onAction” callback is executed when the user changes the checkmark.
I modified the comboBox slightly as well by introducing the “sizeString” attribute. The comboBox will be as wide as necessary to display that particular string in the text field.
Last but not least, I added the QuickStyles gallery as a large button and the Change Styles button as well. I should note that ChangeStyles is a dynamicMenu and the RibbonX XML Schema requires a dynamicMenu to have a “getContent” callback. As this gallery is provided by Microsoft, I obviously cannot provide a callback function for this, which is why I specified an empty attribute. However, a string of length 0 (”") is not accepted by Office. Therefore the string is of length 1 (” “). Quite corky and I’ll submit a bug on this.
These are all the changes needed in the RibbonX code. I won’t discuss the implementation of the “getVisible” and “getPressed” callbacks, as that is straightforward. The add-in saves the visibility state in the registry and retrieves it from there as well. When the user clicks the checkbox, “onAction” is triggered. “onAction” saves the changed state to the registry and invalidates all my custom UI elements. Therefore all callbacks are executed again and the change in visibility is applied. Apparently Beta 2 still has bugs with the dynamicMenu. Once the “getVisible” callback was executed one time for it, it will not be called again. That means, if you click the checkbox, the “Change Styles” button will stay (if it was visible at startup) or never appear (if it was not visible at startup). Also, it is rather curious that “getVisible” is executed whenever you switch from another window to Word.
Conclusion
As conclusion, I know for sure that I will do all my add-ins in C#. VB.NET is somewhat unwieldly to sue for me. To give you a taste what the add-in looks like, here is a screenshot of the add-in with the Styles group visible:

If you hide the MS-group, the Home tab now looks like this:

All the screenshots were taken on my desktop with a 1280×1024 resolution. The tab will look quite different with a lower resolution.
Downloads
Edited to update setup file to V1.2.1 which runs on .Net Framework 2.0 and supports Vista. No source code changes made
Edited to include workaround for a Word 2007 bug into V1.2.2

June 25th, 2006 at 11:15
I installed StyleChooser V1.2.2. It’s much better than the standard 2007 Style Chooser. However it seems there is a potential issue with macros. I have a VB macro that contains a sequence of style replacement code like this:
Selection.Find.ClearFormatting
Selection.Find.Style = ActiveDocument.Styles(”X1″)
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Style = ActiveDocument.Styles(”X2″)
Selection.Find.Execute Replace:=wdReplaceAll
I have lots of Word documents that I have to go through to replace old style names with style names based on a new convention. I attach the new template that contains the new style names to the document and then execute the macro. Up until yesterday it worked fine. Today it doesn’t work any longer. Could it be related to installing StyleChooser V1.2.2 this morning?
I stepped through the macro VB code in debugging mode and it executes every line in the code, it just doesn’t replace the style.
June 25th, 2006 at 11:44
Hi Dietmar,
what happens when you disable the add-in with Word Options, Add-Ins, Go button and then uncheck it? Does your VBA code work then?
Patrick
June 26th, 2006 at 12:41
Patrick,
I just tested it again (without unchecking the add-in). The macro works ok now. I tested the same files that I opened yesterday and no changes were made to either the Word files or the macro since yesterday. I am not sure why the macro didn’t execute yesterday - however I am disbanding the theory that it had anything to do with the add-in.
Here are two ideas on making StyleChooser even more valuable: The drop-down box displays the active style. It would be great if I can right-click in that drop-down box and it goes right to the ‘Modify Style’ pop-up for that specific style. Also, it would be a time saver if the ‘Quick Styles’ Button remembers the last selected style and allows to apply it to a paragaph with a single click. This would be the same way as the ‘Text Highlight Color’ button works: Clicking on the icon applies the last selected color, clicking on the drop-down icon to the right of it drops down the box of color choices.
Dietmar
June 27th, 2006 at 12:11
Dietmar,
glad it wasn’t my add-in!
Right-clicking the drop-down box won’t be possible, as there is no way to program the right-click with RibbonX. Right-clicking an item in the Ribbon is reserved for the small right-click menu that lets you add an item to the QAT.
The Quick styles button is nothing else than the standard Styles gallery of 2007. Therefore it is an MS control that I just throw in there. There is nothing I can do about its behavior.
Patrick
March 8th, 2007 at 12:29
Patrick,
Do you know the customUI XML has an element for something like the Word 2007 Style Chooser? I would like to create something like that and I am wondering if that is one of the controls that is available to developers to use?
Thanks,
Michael
March 8th, 2007 at 12:59
Michael,
no need to write something like this. Microsoft readded a style choosing combobox in a later beta, which made this add-in obsolete. The control they redadded has the idMso StyleGalleryClassic.
Patrick
June 10th, 2007 at 22:30
thanks