Results 1 to 8 of 8
  1. #1
    Lounger
    Join Date
    Mar 2010
    Location
    Texas
    Posts
    49
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Finding Direct Formated Styles

    I’m trying to get a list of a all styles in a document. However, some styles are not named other than by their direct formatting notation (such as “16 pt. Italic, Shadow”). These styles do not show up in Find or through (the following) code. Other variations include direct modification of an existing style (“List2 + Bold”), or part of a Normal style. They do appear in the Styles Pane but not the Import/Export Organizer.

    How can I find these programmatically?

    Code:
    Public Sub MyFindStyles()
        On Error GoTo MyErrorHandler
       
        Dim currentDocument As Document
        Set currentDocument = ActiveDocument
       
        Dim myStyle As Style
        For Each myStyle In currentDocument.Styles
            Debug.Print myStyle.NameLocal
     
            DoEvents
        Next
       
        Exit Sub
     
    MyErrorHandler:
        MsgBox "MyFindStyles" & vbCrLf & vbCrLf & "Err = " & Err.Number & vbCrLf & "Description: " & Err.Description
    End Sub

  2. Get our unique weekly Newsletter with tips and techniques, how to's and critical updates on Windows 7, Windows 8, Windows XP, Firefox, Internet Explorer, Google, etc. Join our 480,000 subscribers!

    Excel 2013: The Missing Manual

    + Get this BONUS — free!

    Get the most of Excel! Learn about new features, basics of creating a new spreadsheet and using the infamous Ribbon in the first chapter of Excel 2013: The Missing Manual - Subscribe and download Chapter 1 for free!

  3. #2
    WS Lounge VIP
    Join Date
    Mar 2006
    Location
    Maryland, USA
    Posts
    677
    Thanks
    17
    Thanked 57 Times in 50 Posts
    I don't know how to find them programmatically, but they are not styles. Instead they are styles with direct formatting. The ones you see like “16 pt. Italic, Shadow” are formatting on Normal. As far as I've been able to tell, the others always show the style name first, then +, then the formatting description. If the style + formatting is used many times, you may want to make a style. In many cases they would have to be converted to character styles, though.

    HTH,
    Pam

  4. #3
    Star Lounger
    Join Date
    Feb 2011
    Location
    Washington, DC
    Posts
    87
    Thanks
    5
    Thanked 5 Times in 5 Posts

    And if you don't want to see them

    ....turn off Keep Track of Formatting in the options. Then you will see only actual styles in the list.

    - Jessica

  5. #4
    Lounger
    Join Date
    Mar 2010
    Location
    Texas
    Posts
    49
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Thanks! That is good to know. But that's the very reason I want to find them programmatically: We shouldn't be having direct-formatted modified named styles. It sounds like there is no way to programmatically find these deviations?

  6. #5
    Star Lounger
    Join Date
    Feb 2011
    Location
    Washington, DC
    Posts
    87
    Thanks
    5
    Thanked 5 Times in 5 Posts
    You would have to go through each paragraph and compare the actual formatting of the paragraph to the base format. Here is something that sort of does it. I can't remember where I got, and I may even have either written or tweaked it myself, though I doubt it. It compares specific aspects of the paragraph's actual format to the formatting in the basic paragraph style. It adds comments on the non-conforming paragraphs. You could do something else to them, of course.

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

    With ActiveDocument
    For Each p In .Paragraphs
    ' line below ignores those end of table row things that are strange

    If Not IsParaEndOfRow(p) Then
    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

    If p.Range.ParagraphFormat.LeftIndent <> .Styles(sStyle).ParagraphFormat.LeftIndent Then
    sCommentText = sCommentText & "Left Indent. "
    End If
    If p.Range.ParagraphFormat.OutlineLevel <> .Styles(sStyle).ParagraphFormat.OutlineLevel Then
    sCommentText = sCommentText & "outline level. "
    End If
    ' Additional items here
    'If p.Range.ListFormat.ListTemplate <> .Styles(sStyle).ListTemplate Then
    'sCommentText = sCommentText & " listTemplate "
    'End If

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

    End If
    Next p
    End With
    End Sub

  7. #6
    Lounger
    Join Date
    Mar 2010
    Location
    Texas
    Posts
    49
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Thanks for the code! Perhaps not the ideal solution but it addresses it.

  8. #7
    Star Lounger
    Join Date
    Feb 2011
    Location
    Washington, DC
    Posts
    87
    Thanks
    5
    Thanked 5 Times in 5 Posts

    Possible options

    Sure. You could replace the line that creates the comment with lines that reset the paragraph and character formatting, if you're feeling brave and don't want to approve each change.

    Replace this:

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

    with something like this:

    If sCommentText <> "" Then
    .Comments.Add p.Range, sCommentText
    else

    p.Range.ParagraphFormat.Reset
    p.Range.Font.Reset

    End If

  9. #8
    5 Star Lounger kmurdock's Avatar
    Join Date
    Feb 2003
    Location
    Pacific Grove, California, USA
    Posts
    666
    Thanks
    3
    Thanked 19 Times in 18 Posts
    Quote Originally Posted by jweissmn1 View Post
    ....turn off Keep Track of Formatting in the options. Then you will see only actual styles in the list.
    I've recently discovered that in Word 2010 that even with Keep Track of Formatting turned off, you'll still see styles like "Heading 1 + Bold" in the styles pane. To really turn this off, open the Styles Pane and click Options... at the bottom of the pane. Then uncheck any checked boxes in the "Select formatting to show as styles:" section. (Thanks to Hans V!) Make sure you click the radio button to turn it off for all documents using the template.

    Manual formatting is sometimes mandatory -- you can't eliminate its use. Occasionally, you just have to adjust a single heading's space above in order to look right... Unfortunately, without turning off the above, this will show up as a "style" even if it only appears once. I think of it as an extension of the format painter and just one more tool that MSFT provided to undermine their very powerful styles feature -- and all the more reason to turn this off.

    Best, Kim

    P.S. Just checked -- this is true for Word 2007, too.
    Last edited by kmurdock; 2012-04-28 at 13:40.

Tags for this Thread

Posting Permissions

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