Range.MoveUntil is not doing what I need...Any suggestions?

D

dvdastor

Hi All,

I have a document that contains multiple instances of the following
tag: [Test ID=xxxx] (where xxxx is a numeric variable). Each "Test"
tag signals the beginning of a section that I need to perform some
procedures in.

In some instances, I need to remove a Test if that ID has already been
worked.

What I am trying to do for those cases is locate the [Test ID=xxxx] tag
and set the "[" as my Range.Start. I then need to find the next
instance of "[Test ID=" and set the "[" as the Range.End. Once I have
that Range selected, I can delete it and move on.

I was hoping MoveUntil("[Test ID=") would do the trick, but it does not
work like that. As you know, it just stops at the next instance of
"[".

Does anyone have any suggestions on how to find one item, set that as
the start and then find the next item and set that as the end?

Thanks!
 
H

Helmut Weber

Hi,

like this:

' ----
Sub test4444()
ResetSearch
Dim rDcm As Range
Dim rTmp As Range
Set rDcm = ActiveDocument.Range
Set rTmp = Selection.Range
With rDcm.Find
.Text = "[Test ID="
If .Execute Then
rTmp.Start = rDcm.Start
If .Execute Then
rTmp.End = rDcm.Start
rTmp.Select ' for testing
End If
End If
End With
ResetSearch
End Sub
' ---
Public 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
' plus some more if needed
.Execute
End With
End Sub


Greetings from Bavaria, Germany

Helmut Weber, MVP, WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
D

David Sisson

MoveUntil collapses the range. You need to extend it. Use MoveEndUntil
instead.

Sub FindNextOccurance()
Dim Rng As Range
Dim Counter As Long

Set Rng = ActiveDocument.Content

Do
With Rng.Find
.Text = "[Test ID=xxxx]"
.Execute
End With
If Rng.Find.Found Then
Rng.MoveEndUntil "[", wdForward
Rng.Select 'for testing
'
'Process MyRange
'
'Move range pointer for next find
Rng.Start = Rng.End
'For Testing
Counter = Counter + 1
End If

Loop While Rng.Find.Found

MsgBox Counter

End Sub

As far as detecting previous sections, you'll have to grab the ID
Number (Mid(Rng, 10, 4)) and store it in an array to compare to the
others.

Good luck,
David
 

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