Results 1 to 12 of 12
  1. #1
    Lounger
    Join Date
    Apr 2005
    Location
    Silver Spring, Maryland, USA
    Posts
    36
    Thanks
    0
    Thanked 1 Time in 1 Post

    Detect text with character styles applied (Word 2003)

    I'm looking for a way to detect whether a paragraph has a character style (not direct formatting, an actual defined character style) applied to it. I'm actually looking to find all instances where a character style is applied in the main body of a document, so it doesn't have to operate at the paragraph level if that's not the best way. Do I need to do some kind of find with the character style? I'd prefer not to, since I need to iterate through paragraphs for another purpose in the same macro, and would rather not have to go through the document twice.

    Any hints for me?

    Thanks.

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Detect text with character styles applied (Word 2003)

    You can use the Find method to find instances of a style (whether it be a paragraph style or character style), but although you can search within a specific paragraph, it's not easy to restrict repeated searches for the same character style to that paragraph. It's easier to search through the entire document.

    Example:
    <code>
    Dim rng As Range
    Set rng = ActiveDocument.Content
    With rng.Find
    .Text = ""
    .ClearFormatting
    .Format = True
    .Style = "Emphasis"
    .Wrap = wdFindStop
    Do While .Execute
    ' rng is now the "found" range
    Debug.Print rng.Text
    Loop
    End With</code>

  3. #3
    Lounger
    Join Date
    Apr 2005
    Location
    Silver Spring, Maryland, USA
    Posts
    36
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Detect text with character styles applied (Word 2003)

    Hmm. I'm trying to find manual formatting in the doc. So if I find a paragraph in my main loop that looks for formatting in a paragraph that doesn't match the official paragraph style for that paragraph, I could search for that character style in the suspect paragraph's range. I could temporarily reset the found range to the base paragraph style, then test the paragraph again to see if it still has nonmatching font/bold. If it does, then check again for the character style...etc.

    Am I barking up yet another wrong tree here?
    Thanks.

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Detect text with character styles applied (Word 2003)

    It would be a bit tricky to get it right, but it's worth a try...

  5. #5
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Detect text with character styles applied (Word 2003)

    Have a look at the code in the attachment to <post#=722,878>post 722,878</post#>

    This uses Find to locate all cases of character styles in the paragraph. It then resets all "manual" formatting and reapplies the attributes from any character formats that were there originally.

    The relevant code can be found in the form called formResetManualFormatting, in routine ResetParagraph

    StuartR

  6. #6
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts

    Re: Detect text with character styles applied (Wor

    In Word 2007 you could do a search for the style "Default Paragraph Font". This returns hits on a paragraph by paragraph basis that includes manual formatting but excludes other Character Styles.

    I think that could be used to reveal where manual formatting has been used. If your code looped by finding first "Default Paragraph Font" and then searched inside that found range for character formatting discrepancies then you should be able to find most occurrences.

    Another possibility is to have a look at the XML or RTF version of the document - you may find something useful there that would denote manual formatting as distinct from character formatting. When I have a look at the xml version I can see that a character style has a syntax like <w:rStyle w:val="CharStyleName"/> tag whereas local colouring might look like <w:color w:val="C0504D"/> and local italic is <w:i/>
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  7. #7
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,055
    Thanks
    2
    Thanked 417 Times in 346 Posts

    Re: Detect text with character styles applied (Word 2003)

    Hi Jessica,

    If you're trying to find manual formatting in the doc, then testing for the presence of various character styles won't be enough - bolding a character, for example, via hard-formatting doesn't of itself invoke a character style.

    What I think you'll need to do is to run multiple loops through the document testing for the presence of a given font attribute (eg bold, italic, underline point size, colour, background colour, super/subscript, hidden, etc, etc) in the document, then test whether that attribute belongs to the paragraph style for the paragraph(s) in which it is found.
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  8. #8
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Detect text with character styles applied (Word 2003)

    Paul,

    That's exactly what the Macro I pointed to does.

    StuartR

  9. #9
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,055
    Thanks
    2
    Thanked 417 Times in 346 Posts

    Re: Detect text with character styles applied (Word 2003)

    Hi Stuart,

    True. I was misled by your reference to locating "all cases of character styles in the paragraph", into thinking your reference to styles had the same meaning as MS gives it, as distinct from hard formatting.
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  10. #10
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Detect text with character styles applied (Word 2003)

    Paul,

    The code does both, it first locates all Character styles that have been appled to the paragraph, and stores information about the sytle name and range. It then resets selected character attributes to the defaults for the paragraph style, and finally it reapplies any character styles that were previously in place.

    StuartR

  11. #11
    Lounger
    Join Date
    Apr 2005
    Location
    Silver Spring, Maryland, USA
    Posts
    36
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Detect text with character styles applied (Word 2003)

    Right - I do want to find deviations that are NOT part of character styles; it's okay for my authors to apply character styles, but not okay for them to do direct manual formatting.

    So when I do the runthrough and check for cases where the text of a paragraph has different format than the style applied to the paragraph, I want to ignore the character styles. So once I find a chunk of text that has a format difference, I want to see if it is different because a character style is applied to it.

    Unless, of course, a totally different approach would work better in Word 2003....

    Thanks.

  12. #12
    Lounger
    Join Date
    Apr 2005
    Location
    Silver Spring, Maryland, USA
    Posts
    36
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Detect text with character styles applied (Word 2003)

    Thanks, Stuart. And sorry about asking the same question in two places. My bad.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •