Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / August 2005

Tip: Looking for answers? Try searching our database.

How to select a range based on tags

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jeff - 30 Aug 2005 22:47 GMT
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.
Greg Maxey - 30 Aug 2005 23:00 GMT
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

Signature

Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

> 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.
Klaus Linke - 31 Aug 2005 00:50 GMT
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

>> 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.
Jeff - 31 Aug 2005 18:07 GMT
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

> Try something like:
>
[quoted text clipped - 86 lines]
> > within a document to use for either the start or end range value.
> > Any suggestions would be greatly appriciated.

Rate this thread:






 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.