Results 1 to 15 of 15
  1. #1
    New Lounger
    Join Date
    May 2003
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Macro to verify styles (XP)

    I have a macro (see below) that cruises through a document and looks at styles. If the style found doesn't match a list of styles, then this is written to another document. I use this to make sure long documents are correctly formatted.

    This works well, *except* for cases where the style is something like "Normal + font: bold centered". Apparently this returns to the macro as style "Normal". These extra formatting no-nos are exactly what I wanted to catch. Any ideas on how to fix the following code:

    Option Explicit ' must declare variable before you can use them
    Sub JUSTIS2_Check_Styles()
    ' with a document open and active, run this macro and the macro will:
    ' - create a new document
    ' - run through the first document and look at each paragraphs style
    ' - if the style is not part of the official list of styles allowed then
    ' - write this style name to the new document
    ' - end with leaving the new document open for inspection
    '
    Dim thisDoc As Document ' declare a variable
    Dim outDoc As Document ' declare a variable
    Dim para As Paragraph ' declare a variable
    Dim strStyle As String ' declare a variable
    Dim dct As Scripting.Dictionary ' must reference "Microsoft Scripting Runtime"; look at Tools | References

    Set dct = New Scripting.Dictionary ' create a new instance of the dictionary object

    On Error Resume Next ' just ignore errors and keep on trucking

    dct.Add "Normal", 1 ' add a style to the dictionary. The second parameter (1) is of no importance - we won't use it
    dct.Add "Body Text", 2
    dct.Add "Heading 1", 3
    dct.Add "Heading 2", 4
    dct.Add "Heading 3", 5

    Set thisDoc = ActiveDocument ' set the thisDoc variable so it refers to the open, active document
    Set outDoc = Word.Application.Documents.Add ' create a new document and point the outDoc variable to it
    outDoc.Activate ' activate your new document; now any actions like typing will go into this document
    Selection.HomeKey unit:=wdStory, Extend:=wdMove 'go to top of doc

    For Each para In thisDoc.Paragraphs ' for every paragraph in the main document
    strStyle = para.Style ' save the style to a string variable
    If dct.Exists(strStyle) = False Then ' is that style in our dictionary of approved styles?
    Selection.TypeText (strStyle) ' NO - type the style name into the working document
    Selection.TypeParagraph ' and add a paragraph
    End If
    Next para ' go back to top of "For each" loop with next paragraph
    ' all out of paragraphs, so the loop ends and
    End Sub ' this macro ends

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

    Re: Macro to verify styles (XP)

    If you apply direct formatting to a bit of text, the Style dropdown list in Word 2002 will reflect this: if you make a word bold, the Style dropdown list displays Normal + Bold. This does not mean, however, that you have created a new style named Normal + Bold. The style of the text is still Normal, with bold formatting superimposed.

    If you activate the Styles and Formatting task pane, you will see that Normal + Bold is listed among available formatting, but there is no paragraph style symbol or character style symbol next to it.

  3. #3
    New Lounger
    Join Date
    May 2003
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Macro to verify styles (XP)

    OK, then how do I accomplish my goal? I want a macro to find all formatting "extras"?

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

    Re: Macro to verify styles (XP)

    That is a lot of work. You would have to loop through the characters in the document to compare the character formatting properties of each character to the style properties, and also loop through all paragraphs to compare the paragraph formatting properties of each paragraph to those of the style.

  5. #5
    New Lounger
    Join Date
    May 2003
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Macro to verify styles (XP)

    OK. I was hoping these format changes were in the Word Object Model somewhere. Since Word shows them all nicely organized in the styles and formatting panel and you can "select all 6 instances", I was hoping I could go through some collection of formats, maybe looking for the plus sign.

  6. #6
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Macro to verify styles (XP)

    Let's suppose you are only interested in paragraph styles, not character styles. It's possible that if you compare the officially assigned style property, such as Bold, with its actual value for that paragraph you would be able to detect a discrepancy. Word might not be consistent about this, but often the following is true:

    All Bold = True
    None Bold = False
    Mixed = 999999 (or something like that)

    But this would be a somewhat arduous process, checking all the relevant types of direct formatting, and what if a word was bolded for emphasis?? Hmmm...

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

    Re: Macro to verify styles (XP)

    You can do this fairly easily with the following manual steps...
    <UL><LI>Select all the text in the document (control A)
    <LI>Reset all character formatting (control Space)
    <LI>Reset all paragraph formatting (Control Q)
    <LI>Save this document with a new name
    <LI>Use Tools > Compare documents to compare the original with this new version[/list]I just tested this and it shows all the formatting that was not part of a style as tracked changes.

    StuartR

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

    Re: Macro to verify styles (XP)

    Very smooth solution Stuart. <img src=/S/clapping.gif border=0 alt=clapping width=19 height=23>

    One problem with this approach is that wiping the local character formatting also wipes character styles. This is my number one gripe with using character styles in Word. Depending on the expected document formatting this may not be a problem.
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  9. #9
    Uranium Lounger
    Join Date
    Dec 2000
    Location
    Los Angeles Area, California, USA
    Posts
    7,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Macro to verify styles (XP)

    I agree (on both counts). <img src=/S/smile.gif border=0 alt=smile width=15 height=15> It would be nice if Ctrl+Spacebar only wiped out direct character formatting, not character styles. You could still get rid of character styles, if you wished, by Find/Replace (Find: *, Replace: Style/Default character style).

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

    Re: Macro to verify styles (XP)

    I definitely agree with the point about character formatting, I never use character formatting because of this particular "design feature".

    A long time ago I posted a macro that would reset all formatting to the underlying style, handling character formatting correctly. It was a pig to get right and involved searching for every character style in the document and storing the start and end of the range in an array so I could reapply them later.

    If this would be helpful to the original poster then I could re-post it.

    StuartR

  11. #11
    Lounger
    Join Date
    Dec 2000
    Location
    Dallas, Texas
    Posts
    40
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Macro to verify styles (XP)

    I don't know about the original poster, but I would sure appreciate seeing your macro. It sounds like the perfect answer to a situation I'm constantly encountering.

    Thanks

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

    Re: Macro to verify styles (XP)

    Here is a template that contains the macro (and associated form).

    Go to Tools > Macro > Macro and run the macro called ResetManualFormatting.

    StuartR
    Attached Files Attached Files

  13. #13
    Lounger
    Join Date
    Dec 2000
    Location
    Dallas, Texas
    Posts
    40
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Macro to verify styles (XP)

    Thanks for your macro and form. However, it doesn't seem to do what I was expecting.

    My situation is this:
    When a document is forwarded to writers, specific styles are applied to the empty paragraph where they will provide content--say Response1. When the document is returned, many Response1 styles have been replaced by Response1 Char or even Response1 Char Char. I have not been able figure out how manage to do this or where the duplicate styles are coming from. I need to replace these Char styles with the original styles and make sure they are removed from the document.

    All documents are done in Word 2002. I have attached an example of this.
    Attached Files Attached Files

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

    Re: Macro to verify styles (XP)

    Those styles are typically created when a user selects some of the text in a paragraph and applies a paragraph style. Word creates a new character style, based on the paragraph style, with "char" at the end of the name.

    I know this creation of styles can be disabled, because I did so a long time ago. I think it was Tools > Options > Keep track of styles, but I'm not sure as I can't recreate the issue now.

    StuartR

  15. #15
    Lounger
    Join Date
    Dec 2000
    Location
    Dallas, Texas
    Posts
    40
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Macro to verify styles (XP)

    I suspected the same thing, but I cannot seem to duplicate it, no matter how I apply a style. It doesn't seem to matter if Keep track of styles is on or off.

    This is a puzzler to me.

    Thanks for your help. I will let you know if I ever do get to the bottom of this.

Posting Permissions

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