MS Office Forum / Word / Programming / March 2008
Cleaning out all linked "Char"-Styles in one go
|
|
Thread rating:  |
andreas - 21 Mar 2008 20:32 GMT Dear Experts:
Below code which I found on the Internet creates a new style ("Style1"), links the "Char" style that bases on "Heading 2" to this new style, then deletes the new style.1. This way one can easily get rid of these awful linked character styles. These rogue characther styles (e.g. Heading 1 Char Char) are automatically created if one tries to apply a paragraph style to just part of a paragraph.
The trouble is, I got a document which has literally hundreds of these (rogue) linked Character Styles. Is there a way to alter below code to include all linked "Char" Styles regardless of their exact name (e.g. Heading 1 Char Char Char or Table Body Text Char Char Char Char or just Char Char).
Help is much appreciated. Thank you very much in advance.
Regards, Andreas
Sub DeleteHeading2Char() Dim styl As Word.Style, doc As Word.Document Set doc = ActiveDocument
Set styl = doc.Styles.Add(Name:="Style1") On Error Resume Next doc.Styles("Heading 2 Char").LinkStyle = styl styl.Delete End Sub
Klaus Linke - 22 Mar 2008 05:35 GMT Hi Andreas,
Which version do you use? Word 2007 has a new property for styles, .Linked, which can make it easier.
In previous versions, I tested each character(!) style on whether it was linked to a paragraph style. Then if that wasn't the "Normal" (Standard) style, I checked the linked paragraph style whether it was linked to the character style. Any regular character style will report "Normal" as its linked style (... pretty weird, but it's not my design).
It seems to me, if those two conditions are met, it's a Char style. The name doesn't matter much... In German, it's "Zchn" rather than "Char", in other countries other names. "Char Char" styles are actually paragraph styles created by one of the several bugs involved. Also, any built-in or user-defined style (without "Char" in its name) can become a linked style of some paragraph style (sometimes it seems spontaneously by bugs).
So after that test, I'd link the Char style to the Normal paragraph style. That turns it into a "plain" character style that you can see in the interface (which you usually can't before, in Word 2003).
If you simply delete it (or link it to some new paragraph style and delete that paragraph style, as your macro does), you'll loose all the formatting that was done by the character style, and it'll revert to the default paragraph font.
Maybe that's no problem for you, but in the documents I get, it often does. So once the character styles are visible in the styles pane, I'll have to go through them and decide whether to turn them into something else, or remove them (and their formatting).
Regards, Klaus
> Dear Experts: > [quoted text clipped - 25 lines] > doc.Styles("Heading 2 Char").LinkStyle = styl > styl.Delete End Sub andreas - 22 Mar 2008 11:05 GMT > Hi Andreas, > [quoted text clipped - 63 lines] > > doc.Styles("Heading 2 Char").LinkStyle = styl > > styl.Delete End Sub- Hide quoted text -
> - Show quoted text - Dear Klaus,
thank you for your your very detailed description of the problem with these rogue "Char" Styles. I was not aware of all of what you said, hence it is very helpful to me. Ok, the following code links a specific "Char" style to the normal style.
ActiveDocument.Styles("BodyText Char Char").LinkStyle = ActiveDocument.Styles(wdStyleNormal).
My initial problem remains. Is it possible to link all "Char"-Styles to the Normal style in one go? I try to avoid to enter dozens and dozens of these Char-Styles one after the other in between the parentheses of above code.
Help is much appreciated. Thank you in advance. Regards, Andreas
Klaus Linke - 25 Mar 2008 11:50 GMT I first save the doc as XML, close and reopen it (in Word 2003 that is).
The Char styles that start with a blank (like " Zchn Zchn1"...) can't be gotten rid of at all with a macro, I think. And they can cause problems for my macro, often crashing Word.
If you save as XML and then close/re-open, they are fixed, though.
The macro writes the styles that had been linked and possible errors to the Debug window.
You could immediately delete the Char styles (myStyle.Delete), but you'd loose all the formatting... I would not recommend that, except if you own the docs and you really don't mind.
Regards, Klaus
Sub LinkStyleToRegularCharStyle() Dim myStyle As Style Dim myStyleLinkStyle As Style For Each myStyle In ActiveDocument.Styles Set myStyleLinkStyle = myStyle.LinkStyle ' Debug.Print myStyle, myStyleLinkStyle If myStyleLinkStyle <> _ ActiveDocument.Styles(wdStyleNormal) _ And myStyle.Type = wdStyleTypeCharacter Then If myStyleLinkStyle.NameLocal <> "" Then If myStyleLinkStyle.Type = wdStyleTypeParagraph And _ myStyleLinkStyle.LinkStyle = myStyle Then Debug.Print "Para style: " & myStyleLinkStyle, _ "Char style: " & myStyle myStyle.LinkStyle = _ ActiveDocument.Styles(wdStyleNormal) If myStyle.LinkStyle <> ActiveDocument.Styles(wdStyleNormal) Then Debug.Print "Char style still linked erroneously!" End If myStyleLinkStyle.LinkStyle = _ ActiveDocument.Styles(wdStyleNormal) If myStyle.LinkStyle <> ActiveDocument.Styles(wdStyleNormal) Then Debug.Print "Para style still linked erroneously!" End If
End If End If End If Next myStyle End Sub
Regards, Klaus
On Mar 22, 5:35 am, "Klaus Linke" <i...@fotosatz-kaufmann.de> wrote:
> Hi Andreas, > [quoted text clipped - 68 lines] > > doc.Styles("Heading 2 Char").LinkStyle = styl > > styl.Delete End Sub- Hide quoted text -
> - Show quoted text - Dear Klaus,
thank you for your your very detailed description of the problem with these rogue "Char" Styles. I was not aware of all of what you said, hence it is very helpful to me. Ok, the following code links a specific "Char" style to the normal style.
ActiveDocument.Styles("BodyText Char Char").LinkStyle = ActiveDocument.Styles(wdStyleNormal).
My initial problem remains. Is it possible to link all "Char"-Styles to the Normal style in one go? I try to avoid to enter dozens and dozens of these Char-Styles one after the other in between the parentheses of above code.
Help is much appreciated. Thank you in advance. Regards, Andreas
andreas - 26 Mar 2008 20:43 GMT > I first save the doc as XML, close and reopen it (in Word 2003 that is). > [quoted text clipped - 149 lines] > > - Show quoted text - Hey Klaus,
great, this did the trick. Thank you very much for the nice code.
Regards, Andreas
Klaus Linke - 27 Mar 2008 16:07 GMT > great, this did the trick. Thank you very much for the nice code. Thanks for the feedback! I just re-worked the code the other day, so it hasn't been tested much. Although I've used similar code for months.
Gruß, Klaus
Graham Mayor - 22 Mar 2008 08:27 GMT Have you seen - http://support.microsoft.com/kb/902064
 Signature <>>< ><<> ><<> <>>< ><<> <>>< <>><<> Graham Mayor - Word MVP
My web site www.gmayor.com Word MVP web site http://word.mvps.org <>>< ><<> ><<> <>>< ><<> <>>< <>><<>
> Dear Experts: > [quoted text clipped - 25 lines] > doc.Styles("Heading 2 Char").LinkStyle = styl > styl.Delete End Sub andreas - 22 Mar 2008 11:06 GMT > Have you seen -http://support.microsoft.com/kb/902064 > [quoted text clipped - 37 lines] > > - Show quoted text - Dear Graham,
ok, this solves half of my problem. Thank you. In my response to Klaus I elaborate on my initial problem. Thank you, Regards, Andreas
Klaus Linke - 25 Mar 2008 11:11 GMT This "hotfix", as far as I'm aware, just hides the Char styles again after they became visible (which usually happens/happened when you edited the doc in an older version).
I'm usually trying the opposite... to make them visible.
I just got a doc last week with lots of Char styles. If I directly export it, it wreaks havoc with the layout since half the Headings are true headings, and the other half are several paragraph styles with "Heading # Char" applied on top... sometimes applied to all the text, sometimes to part of the text.
Unfortunately I have to fix the mess, mostly manually paragraph by paragraph... But unless I see the Char styles in the first place, I would have no chance to do that.
Regards, Klaus
> Have you seen - http://support.microsoft.com/kb/902064 > [quoted text clipped - 27 lines] >> doc.Styles("Heading 2 Char").LinkStyle = styl >> styl.Delete End Sub
|
|
|