Consulting

Results 1 to 5 of 5

Thread: [VBA] highlight number listed headings with yellow

  1. #1

    [VBA] highlight number listed headings with yellow

    My word document has numbered titles in the following structure:

    ***XX ***X ***X
    ***X *********X *********

    1. *********X
    1.1 *********X
    ***X *********X *********
    ***X *********X *********
    1.2 ***X *********X *********
    ***X *********X *********

    1.2.1 ***X *********X *********
    ***X *********X *********

    2. *********X
    2.1 *********X

    ***X *********X *********
    2.2 ***X *********X *********
    2.3 ***X *********X *********


    ...

    I would like to know how to color those bold line with yellow background automatically.
    If possible, a re-usable VBA code would be best.

    THank you.

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    This colors the text in the designated style. Is this what you were asking?

    I'm sure the more Word-wise members have ways to improve it, but it seems to work


    Option Explicit
    Sub drv()
        Call HighLightText("Heading 1")
        Call HighLightText("Heading 2", wdColorGreen)
        
    End Sub
    
    Private Sub HighLightText(StyleName As String, Optional ColorName As WdColor = wdColorYellow)
        Selection.HomeKey Unit:=wdStory
        With Selection.Find
            .ClearFormatting
            .Style = ActiveDocument.Styles(StyleName)
            .Text = ""
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindContinue
            .Format = True
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        
            Do While .Execute
                With Selection.Font.Shading
                    .Texture = wdTextureNone
                    .ForegroundPatternColor = wdColorAutomatic
                    .BackgroundPatternColor = ColorName
                End With
            
                Selection.MoveRight Unit:=wdCharacter, Count:=1
            Loop
            
        End With
    End Sub
    Attached Files Attached Files
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    Hi Paul,
    Thanks for the input.
    The code works well after changing "Heading 1" to wdStyleHeading1 or wdStyleHeading2

    However, I find that not all numbered lines are highlighted in my document.
    My document contains 500+ pages. The highlighted lines mostly are within the first 30 pages.
    I have tried adding wdStyleHeading3 and wdStyleHeading4.
    It gets highlighted more but still not applicable to the fullest document.
    Any idea?

  4. #4
    VBAX Expert
    Joined
    Sep 2016
    Posts
    788
    Location
    Option Explicit
    
    
    Sub test()
        Dim p As Paragraph
        Dim l As List
        
        For Each p In ActiveDocument.Paragraphs
            Set l = Nothing
            On Error Resume Next
            Set l = p.Range.ListFormat.List
            On Error goto 0
            If Not l Is Nothing Then
                p.Range.HighlightColorIndex = wdYellow
            End If
        Next
    
    
    End Sub
    Last edited by mana; 07-08-2017 at 08:39 AM.

  5. #5
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    Quote Originally Posted by smallxyz View Post
    Hi Paul,
    Thanks for the input.
    The code works well after changing "Heading 1" to wdStyleHeading1 or wdStyleHeading2

    However, I find that not all numbered lines are highlighted in my document.
    My document contains 500+ pages. The highlighted lines mostly are within the first 30 pages.
    I have tried adding wdStyleHeading3 and wdStyleHeading4.
    It gets highlighted more but still not applicable to the fullest document.
    Any idea?
    Not sure why you needed to add wdStyleHeading1. The sub drv() takes a style name ("Heading 1") as input; I did it that way for flexibility. You could always incorporate that into the HighLightText sub.

    Are you sure that the lines that are being missed are styled with "Heading 1"?

    mana raises a good point - there is a difference between the Font attribute .Shading and the .HighLighter. I used the font attribute approach

    Can you post a small doc (1-2 pages) with examples of what is not being highlighted

    And of course, there's always the very real possibility that my .Find macro was deficient.
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Posting Permissions

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