View Full Version : [VBA] highlight number listed headings with yellow
smallxyz
07-08-2017, 06:08 AM
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.
Paul_Hossler
07-08-2017, 06:45 AM
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
smallxyz
07-08-2017, 07:48 AM
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?
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
Paul_Hossler
07-08-2017, 09:06 AM
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.
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.