Hack 55 Clean Out Linked "Char" Styles![]() ![]() 2003 have a nasty habit of sprouting hidden character styles that are hard to see, let alone eliminate. This hack shows you how to lead a "Char-free" life with Word.In most programs that offer style-based formatting (such as InDesign, FrameMaker, or QuarkXPress), if you try to apply a paragraph style to just part of a paragraph, the entire paragraph is modified to reflect the new style. But in Word, things aren't so simple. In older versions of Word, when you apply a paragraph style to only part of a paragraph, the paragraph retains its original style, but the selected text takes on the character formatting of the paragraph style you tried to apply. That introduces a lot of direct formatting into documents, which can make them difficult to modify and maintain.
formatting of the paragraph style, Word creates a new, hidden character style based on the paragraph style and tacks the word "Char" on the end, such as "Heading 1 Char."Ostensibly, this is an improvement of the behavior of earlier versions of Word. Rather than changing Word to behave like other programs (and thus encouraging the conscientious use of character styles), Microsoft changed Word to behave in a strange, new, andsurprise!poorly documented way. Because the Char styles are linked to the paragraph styles on which they're based, if you later change the paragraph style, the character style also changes. 6.6.1 Spotting Char StylesTo see these bizarre styles in action (assuming you are using a newer version of Word), open a new document and drop in a few paragraphs of placeholder text [Hack #14] .Now select a word or two within one of the paragraphs and apply the "Heading 1" style to the selection. Check your styles, either using the Task Pane or the Styles pull-down menu on the Formatting toolbar. See anything amiss? Well, that's a trick question. There are two things amiss, but you won't see them right away on the Styles pull-down menu or the Task Pane:Word created a new character style based on the formatting properties of the "Heading 1" style.Word has not told you about this new style, which, as you've discovered, doesn't appear with the other styles in the usual places. Now, hold down the Shift key and click on the Styles pull-down menu on the Formatting toolbar. Scroll down to the "H" section, as shown in Figure 6-8, and you'll see the new style. You can also view the linked style from the Reveal Formatting Task Pane. Figure 6-8. The elusive Char style![]() However, if you send your document to someone using an older version of Word that doesn't support these "linked styles," they'll appear right away in the Styles pull-down menu and the Styles and Formatting dialog.Once you cut and paste the styles around your document, among different documents, and back and forth across different versions of Word, something even stranger happens. They evolve. Mutate. Fester. Until your document is rife with monstrosities like the following: Body Text Char Char, Body Text Char1 Char Char, Body Text Char CharSometimes the "Char" extension even gets added to some of your paragraph styles. Does the fun ever stop?Unfortunately, you can't prevent Word from creating these styles. And even if you still use Word 2000, these styles will show up in documents worked on by Word 2002 and 2003 users.The situation gets even stranger when you try to delete these styles. In Word 2000, you at least stand a chance. Since 2000 doesn't have linked styles, you can rename or delete them as needed, just like any other style. But in Word 2002 and 2003the source of these bizarre stylesyou aren't so lucky. When you try to delete "Heading 1 Char," the silence is deafening. Nothing. Not even a dialog admonishing your efforts.So maybe you should try a little VBA? Running the following code in the Immediate window [Hack #2] would seem to be a solution: ActiveDocument.Styles("Heading 1 Char").DeleteWhen you run the code, the dialog shown in Figure 6-9 greets you. But take a look at your document againthe style's gone. Figure 6-9. Deleting the linked style generates a runtime error![]() code in the Immediate window: ActiveDocument.Styles("Heading 1").DeleteYou get your old friend, Runtime Error 4198.Remember, the styles are linked. If you change one, you change bothand that goes for deletion as well. However, you can't delete a built-in style, which is why you're greeted with Runtime Error 4198 (and why nothing happened when you tried to delete it from within Word).So what if you repeat this with a Char style not based on one of Word's built-ins? Then there's nothing stopping Word from deleting both styles and removing all of the formatting from any text that used them. Yikes! Fortunately, there's a fix. With the code in this hack, you can quickly clear out any linked Char styles in your document, without losing any other styles. 6.6.2 The CodeThis code will delete any character style with the word "Char" in it and remove the word "Char" from the name of any paragraph style. Since deleting a linked style also deletes any style it's linked to, the link must first be broken (which is accomplished by linking it to, ironically, the Normal style).
LinkStyle property for styles, if you're using that version of Word, this code will not run unless you comment out one line, as noted within the code. Note that there are two procedures here: the main DeleteCharCharStyles macro and a supporting function named SwapStyles. Both are needed for this hack to work. Sub DeleteCharCharStyles( )The second procedure, SwapStyles, is there because of a scenario that often occurs when documents that have these linked styles go back and forth between different versions of Word. Often, what started as one paragraph stylefor example, Sidebarmay have mutated into two paragraph styles, such as:SidebarSidebar Char Char In that situation, if the code just tries to remove the "Char" strings from the second Sidebar style, an error will be raised because the name Sidebar is already taken. With the SwapStyles procedure, all the text formatted with the second style is modified and formatted with the first, and then the second style is simply deleted. 6.6.3 Running the HackAfter you put both procedures in the template of your choice [Hack #50], and either run it from the Tools dialog or put a button for it on a menu or toolbar [Hack #1].This code affects only styles with the string " Char", including the leading space. If you plan to use this macro to clean your documents, you should avoid deliberately using the string " Char" in any of your styles. However, feel free to begin a style name with Char, as in "CharacterStyleNumberOne." 6.6.4 Hacking the HackIf you want to delete the linked Char styles but retain the character formatting on the text, use this version of the code. It includes an additional procedure, StripStyleKeepFormatting, that removes the character style applied to the text but retains the formatting defined by that style. Again, if you use an earlier version of Word, you'll need to comment out the line that unlinks the character style, as noted in the code. Sub DeleteCharCharStylesKeepFormatting( )
|