macro to highlight text in carrots

J

jkmar5

Is there a macro that I could use to highlight text in carrots? I would like
the dark gray highlighting color.

For example, here is a section of the document.
<question>
<quest>Having a good attitude about school and work and feeling good about
what you achieve.</quest>
<answer>career wellness</answer>
</question>

I need everything inside the carrots to be highlighted dark gray.

Thanks.
 
K

Klaus Linke

jkmar5 said:
Is there a macro that I could use to highlight text in carrots? I would
like
the dark gray highlighting color.

For example, here is a section of the document.
<question>
<quest>Having a good attitude about school and work and feeling good about
what you achieve.</quest>
<answer>career wellness</answer>
</question>

I need everything inside the carrots to be highlighted dark gray.


In your example, it would be easiest to highlight everything, and then
remove the highlight from the tags with a wildcard replacement:
Edit > Replace, check "Match wildcards",
Find what: \<*\>
Replace with: ((go to Format > Style > Default Paragraph Font))

\< matches "<", \> matches ">", * matches anything else in between.


If you just want to highlight the innermost tags, you could use a more
advanced wildcard replacement:
Find what: \<([!/\<\>]@\>)[!\<\>]@\</\1
Replace with: ((choose some formatting such as Highlight))
Then with the first wildcard replacement given above, remove the manual
formatting from the tags.

[!/\<\>] matches any character except a slash or angle bracket, [!/\<\>]@
matches any number of them.

\<([!/\<\>]@\>) matches any starting tag such as "<quest>". To reuse a
wildcard expression, you can put brackets around it.
You can reuse the first bracketed expression using the wildcard \1.

So in the example, ([!\<\>]@\>) would match "quest>", and \</\1 would match
"</quest>".

[!\<\>]@ makes sure that the text in between the starting and closing tags
don't contain other tags.


Regards,
Klaus
 
G

Greg Maxey

Depends if you want the tags included or excluded:
Sub ProcessExcludingTags()
Dim oRng As Range
Dim myRange As Range
Dim i As Long
Set oRng = ActiveDocument.Range
ActiveDocument.Range(0, 0).Select
With oRng.Find
.Text = "<"
.Wrap = wdFindStop
While .Execute
i = oRng.End
oRng.Collapse direction:=wdCollapseEnd
.Text = ">"
If .Execute Then
Set myRange = oRng.Duplicate
myRange.Start = i
myRange.End = oRng.Start
myRange.HighlightColorIndex = wdGray50
.Text = "<"
oRng.Collapse direction:=wdCollapseEnd
oRng.Select
End If
Wend
End With
End Sub
Sub ProcessIncludingTags()
Dim oRng As Range
Dim pUserHLOption As String
pUserHLOption = Options.DefaultHighlightColorIndex
Options.DefaultHighlightColorIndex = wdGray50
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = "(\<)(*)(\>)"
.Wrap = wdFindStop
.MatchWildcards = True
With .Replacement
.Text = "\1\2\3"
.Highlight = True
End With
.Execute Replace:=wdReplaceAll
End With
Options.DefaultHighlightColorIndex = pUserHLOption
End Sub
 
J

jkmar5

Greg,
Thanks so much for the macro. I'm using the process including tags. It works
awesome and has saved me hours of tedious work.

I'm trying to modify the macro for a similar document, where I want to
highlight everything inside quotation marks ("). I tried changing the <>
marks in the macro to " but I'm getting an error message. Do you have any
ideas?

Thanks again.
 
G

Greg Maxey

You need to be sure someone hasn't inadvertenly left out a open or closed
quote symbol. Try:

Sub ProcessIncludingTags()
Dim oRng As Range
Dim pUserHLOption As String
pUserHLOption = Options.DefaultHighlightColorIndex
Options.DefaultHighlightColorIndex = wdGray50
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = "(" & Chr(34) & ")(*)(" & Chr(34) & ")"
.Wrap = wdFindStop
.MatchWildcards = True
With .Replacement
.Text = "\1\2\3"
.Highlight = True
End With
.Execute Replace:=wdReplaceAll
End With
Options.DefaultHighlightColorIndex = pUserHLOption
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top