Results 1 to 12 of 12
  1. #1
    5 Star Lounger
    Join Date
    Mar 2001
    Location
    New York, NY
    Posts
    922
    Thanks
    2
    Thanked 12 Times in 11 Posts

    VBA DirectFormat Style (Word XP)

    Using VBA, is there a way to determine whether a paragraph in a Word document has had direct formatting applied to it? If Keep Track of Formatting is enabled, the Style pane will show, for example, the style name as Body Text + Bold, indicating the the Bold attribute has been directly applied to the paragraph. Can VBA determine whether any formatting, other than the style itself, has been applied to the paragraph?

    Thanks,
    Richard Barrett

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

    Re: VBA DirectFormat Style (Word XP)

    I think you would have to create a macro that compared each character attribute (font.name, font.size, font.bold etc.) to the attribute of the underlying style.

    I did once write a macro that reset all font and character attributes, to clear any manual formatting, I could let you have a copy as a starting point if you like - it's buried somewhere in the depths of the VBA forum.

    StuartR

  3. #3
    5 Star Lounger
    Join Date
    Mar 2001
    Location
    New York, NY
    Posts
    922
    Thanks
    2
    Thanked 12 Times in 11 Posts

    Re: VBA DirectFormat Style (Word XP)

    I think that would be a long and tedious operation. I've been trying to think of ways to compare the formatting before and after apply the ClearFormatting method to the selection. The Format object/property doesn't seem to be something that I can retrieve and compare.

    Do you know if there's any way to retrieve what Word displays in the style pane when Keep Track of Formatting is enables? If I could compare that to the actual style name, I'd have my answer.

    One of the things that I'm finding in some documents is that users have applied black color to the font. I imagine this is because they at one time colored text red to draw attention to it. AutoColor would be the proper way to get back to black. This is the sort of thing I'm looking for. Word underlines the text in blue wavy lines, but there doesn't seem to be a way for me to "read" that.

  4. #4
    5 Star Lounger
    Join Date
    Mar 2001
    Location
    New York, NY
    Posts
    922
    Thanks
    2
    Thanked 12 Times in 11 Posts

    Re: VBA DirectFormat Style (Word XP)

    I gave this a go... I wrote a little macro that compares the various paragraph formatting properties, font properties, etc. of the applied style to the comparable properties of the paragraph. I then added a comment to the paragraph with the style name, with a + character added for each deviation. I created a function to make the comparison and return the style name with the +'s. It took about 90 seconds for a 55 page document that is FULL of style inconsistenties. That's not so awful, and the code was simple enough to write. I could embellish the + with the description of what deviation was found, but it's easy enough to have a look at the document for details. I'm simply looking to point out the horrible state of the documents.

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

    Re: VBA DirectFormat Style (Word XP)

    Another technique you could try is to
    <UL><LI>Select the entire document (control A)
    <LI>Clear all character formatting (Control space)
    <LI>Clear all paragraph formatting (Control Q)
    <LI>Save the document with a new name
    <LI>Tools > Compare and merge documents[/list]This gives you a document with all the manual formatting highlighted as changes.

    StuartR

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

    Re: VBA DirectFormat Style (Word XP)

    Richard, are you willing to share this macro? I'm trying to something similar.

    Thanks!

  7. #7
    5 Star Lounger
    Join Date
    Mar 2001
    Location
    New York, NY
    Posts
    922
    Thanks
    2
    Thanked 12 Times in 11 Posts

    Re: VBA DirectFormat Style (Word XP)

    I'd be glad to share it... if I can find it. I cobbled that together for a singular purpose quite a while ago, and I'm not sure that I kept it. I'll have a look and let you know.

    Richard

  8. #8
    5 Star Lounger
    Join Date
    Mar 2001
    Location
    New York, NY
    Posts
    922
    Thanks
    2
    Thanked 12 Times in 11 Posts

    Re: VBA DirectFormat Style (Word XP)

    Jessica,

    I couldn't find the macro, but I recall fairly well what I did and I've written some rough that's a starting point. The formatting of each paragraph (left side of = ) is compared to the corresponding formatting of the style that's applied to the paragraph (right side of = ). For each attribute that I check, if a difference is found, I add that "clue" to a string that is added as a comment to that particular paragraph. You can add additional lines of code to test for whatever formatting you want.

    Sub DetectDirectFormatting()
    Dim p As Paragraph
    Dim sCommentText As String
    Dim sStyle As String

    With ActiveDocument
    For Each p In .Paragraphs
    sCommentText = ""
    sStyle = p.Style
    If p.Range.ParagraphFormat.Alignment <> .Styles(sStyle).ParagraphFormat.Alignment Then
    sCommentText = sCommentText & "Alignment. "
    End If
    If p.Range.Bold <> .Styles(sStyle).Font.Bold Then
    sCommentText = sCommentText & "Bold. "
    End If
    If p.Range.Italic <> .Styles(sStyle).Font.Italic Then
    sCommentText = sCommentText & "Italic. "
    End If
    If p.Range.Font.Name <> .Styles(sStyle).Font.Name Then
    sCommentText = sCommentText & "Font. "
    End If
    If p.Range.ParagraphFormat.LineSpacing <> .Styles(sStyle).ParagraphFormat.LineSpacing Then
    sCommentText = sCommentText & "Line Spacing. "
    End If
    If p.Range.ParagraphFormat.SpaceAfter <> .Styles(sStyle).ParagraphFormat.SpaceAfter Then
    sCommentText = sCommentText & "Space After. "
    End If

    ' Additional items here

    If sCommentText <> "" Then
    .Comments.Add p.Range, sCommentText
    End If
    Next
    End With
    End Sub

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

    Re: VBA DirectFormat Style (Word XP)

    Many many thanks. I'll start playing with this tomorrow morning, as they made me do Something Else today.

    My authors do all kinds of things with bullet points and such that they shouldn't, so I'll add stuff to check for numbering and bulleting differences. I wish I could get them to remove the manual bullet and manual indent and manual numbering buttons from their toolbars. I know how to do it, but don't have the authority to impose altered toolbars on the company. Alas.

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

    Re: VBA DirectFormat Style (Word XP)

    Works well! All I had to add something that skipped those dumb end-of-table-row paragraphs, as they don't have a comments property. Thanks again to Andrew Savikas for his quick end-of-row-check function.

    The only part I'm having trouble with is finding cases where someone added bullet points or numbering to a paragraph in a style that shouldn't have them. There's something funky about list styles, to say the least.

    I appreciate all the help I'm getting here. Best board in the known universe.

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

    Re: VBA DirectFormat Style (Word XP)

    Actually, there is a problem. It seems to ignore things that have character styles applied. We have a character style we use for little chunks of text that refer to items on the screen and such..and every instance of it is marked as font and bold changed.

    Is there a way to find character styles? I looked around and didn't see anything.

    Thanks.....

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

    Re: VBA DirectFormat Style (Word XP)

    Jessica,

    You have asked the same question in <post:=723,567>post 723,567</post:> on our VBA forum.

    I will lock this post so we don't get two sets of replies to the same question. If anyone wants to help then please follow the link above to post a reply.

    StuartR

Posting Permissions

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