Find extends the range instead of setting range to new found location

A

Aaron O

I am trying to do a dual find where the first find sets the target
location, and the second find goes in reverse to find the last string
that meets a criterion before the target location. Then I want to
replace parts of the target location with parts of the nearby previous
string. The problem is, when I do the reverse (.forward = false) find
the resulting range is extended from the target location back to the
found string. I need to have two distinct strings to work with, and I
cannot figure out how to turn off this extending behaviour of the
find.

More universally, if anyone knows of a better way to accomplish what I
am trying to do, please let me know.

Thanks! Here's my entire subroutine:

Sub SyncSUB()
Dim SubRng As Range
Dim CommandRng As Range

'Synchronize SUB commands
Set SubRng = Range(0, 0)
With SubRng.Find
.ClearFormatting
.Forward = True
.MatchWholeWord = True
.MatchCase = False
.Wrap = wdFindStop
.MatchWildcards = True
.Execute FindText:="""SUB*"""
End With
SubRng.Select
Set CommandRng = SubRng
With CommandRng.Find
.ClearFormatting
.Forward = False
.MatchWholeWord = True
.MatchCase = False
.Wrap = wdFindContinue
.MatchWildcards = True
.Execute FindText:="\<COMMAND INDEX=* "
CommandRng.Select
Selection = "" & "SUB" & Mid(CommandRng, 16, Len(CommandRng) -
1) & Right(SubRng, 1) & ""
End With


End Sub
 
D

Doug Robbins - Word MVP

After the first find, set a Range variable to the .Range of the document and
then set the .End of the Range variable to the .Start of the Selection.
Then use the InstrRev function on the Range so set to locate the second text
that you want to find.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 
A

Aaron O

Doug, That helped a bit, but now I cannot get the code to advance the
selection. It seems like the find function needs a FindNext mode. As
you can see in my code, I have tried to set the selection forward
manually to get the search to start from the next location, but that
does not help.

New Subroutine:

Sub SyncSUB()
Dim SubRng As Range
Dim CommandRng As Range
Dim ComLookUp As Range
Dim CRStart As Integer
Dim CREnd As Integer
Set SubRng = Range(0, 0)
Set CommandRng = Range(0, 0)
Set ComLookUp = Range(0, 0)
CRStart = 0
CREnd = 0
Range(0, 1).Select
'Synchronize SUB commands
With SubRng.Find
.ClearFormatting
.Forward = True
.MatchWholeWord = True
.MatchCase = False
.Wrap = wdFindStop
.MatchWildcards = True
.Execute FindText:="""SUB*"""
End With
SubRng.Select
Set ComLookUp = Range(0, SubRng.Start)
CRStart = InStrRev(ComLookUp, "<COMMAND INDEX=", , vbTextCompare)
CREnd = InStr(1, Range(CRStart, SubRng.Start), "CMD=",
vbTextCompare)
Set CommandRng = Range(CRStart, CRStart + CREnd - 3)
' CommandRng.Select
Selection = "" & """SUB" & Mid(CommandRng, 16, Len(CommandRng)
- 1) & Right(SubRng, 2)

Do While SubRng.Find.Found = True
Selection.Collapse (wdCollapseEnd)
With SubRng.Find
.ClearFormatting
.Forward = True
.MatchWholeWord = True
.MatchCase = False
.Wrap = wdFindStop
.MatchWildcards = True
.Execute FindText:="""SUB*"""
End With
SubRng.Select
Set ComLookUp = Range(0, SubRng.Start)
CRStart = InStrRev(ComLookUp, "<COMMAND INDEX=", , vbTextCompare)
CREnd = InStr(1, Range(CRStart, SubRng.Start), "CMD=",
vbTextCompare)
Set CommandRng = Range(CRStart, CRStart + CREnd - 3)
' CommandRng.Select
Selection = "" & """SUB" & Mid(CommandRng, 16, Len(CommandRng)
- 1) & Right(SubRng, 2)
Loop

End Sub
 
D

Doug Robbins - Word MVP

It would be easier if I new exactly what it is that you are trying to do,
but the following code will show you how you get a Find operation to repeat
until all the items that there are to be found have been found

Dim myrange As Range
Dim prevline As Range
Dim Findstr As String
Dim Replacementstr As String
Dim Replace
Findstr = InputBox("Insert the text that you want to find.")
Replacementstr = InputBox("Insert the replacement text.")
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(FindText:=Findstr, Forward:=True, _
MatchWildcards:=False, Wrap:=wdFindStop, MatchCase:=False) = True
Set myrange = Selection.Range
Set prevline = Selection.Range.Duplicate
prevline.MoveStart Unit:=wdSentence, Count:=-1
prevline.Select
ActiveWindow.ScrollIntoView Selection.Range, True
Replace = MsgBox("Replace the text?", vbYesNoCancel + vbQuestion)
If Replace = vbYes Then
myrange.Text = Replacementstr
ElseIf Replace = vbNo Then
myrange.Text = myrange.Text
Else
Exit Sub
End If
Selection.Collapse wdCollapseEnd
Selection.MoveRight wdCharacter, 1
Loop
End With


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 
A

Aaron O

Doug,

Thanks for the reply. I got the code to work. Basically, I extracted
the idea of "Selection.MoveRight wdCharacter, 1 " and adapted it to
work for a range variable with "Set SubRng = Range(SubRng.Start + 1,
SubRng.End + 1)". This makes the find move forward, but the
replacement becomes progressively slower as the find goes nearer the
end of the document. The minor performance problem is nothing
compared to manually changing these values though.

Thanks for your help!
 

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