How to select a range based on tags

J

Jeff

I'm trying to code a macro that will look for two character tags (<tag>,
</tag>) and format all the text between the tags. I've seen numerous
examples of how to specify the start and end of a range but can not figure
out how to determine the position of a found string within a document to use
for either the start or end range value. Any suggestions would be greatly
appriciated.
 
G

Greg Maxey

Try something like:

Sub FormatBetweenTags()
Dim startTag As String
Dim endTag As String
Dim x As Integer
Dim y As Integer

startTag:
startTag = InputBox("Enter the start tag (e.g., < )", "Start Tag")
If Len(startTag) < 1 Then
If MsgBox("You did not enter a start tag. Please press OK to go back" _
& " and enter a start tag or press CANCEL to exit.", vbOK +
vbExclamation, _
"Invalid Entry") = vbOK Then
GoTo startTag
Else
Exit Sub
End If
End If
x = Len(startTag) - 1
endTag:
endTag = InputBox("Enter the end tag (e.g., & > )", "End Tag")
If Len(endTag) < 1 Then
If MsgBox("You did not enter an end tag. Please press OK to go back" _
& " and enter a end tag or press CANCEL to exit.", vbOK +
vbExclamation, _
"Invalid Entry") = vbOK Then
GoTo endTag
Else
Exit Sub
End If
End If
y = Len(endTag) - 1
FormatBetweenTags startTag, endTag, x, y
'FormatIncludingTags startTag, endTag
End Sub
Sub FormatBetweenTags(startTag$, endTag$, x As Integer, y As Integer)

Dim oRng As Range
Dim myRange As Range
Dim i As Long

Resetsearch

Set oRng = ActiveDocument.Range
ActiveDocument.Range(0, 0).Select
With oRng.Find
.Text = startTag$
.Wrap = wdFindStop
While .Execute
i = oRng.End
oRng.Collapse direction:=wdCollapseEnd
.Text = endTag$
If .Execute Then
Set myRange = oRng.Duplicate
myRange.Start = i - x
myRange.End = oRng.Start + y
myRange.Font.Italic = True
.Text = startTag$
oRng.Collapse direction:=wdCollapseEnd
oRng.Select
End If
Wend
End With
End Sub
Sub Resetsearch()
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute
End With
End Sub
 
K

Klaus Linke

Hi Jeff,

You can also use a wildcard search. You don't even need to type the text in
the end tag:
Find what: \<(tag\>)*\</\1

The tag element can contain any number of nested elements, or be of any
length (well, I didn't test several MBytes), the expression above will still
find it.

Regards,
Klaus
 
J

Jeff

Greg,

Thank you very much for your response to my post for help on “How to select
a range based on tagsâ€. I was able to easily use your example to accomplish
what I was trying to do. It was a huge help and wanted to let you know that
I greatly appreciate the assistance.

Sincerely,

Jeff
 

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