Find within a specific range

P

prs16001

I'm trying to run a macro over a long document but only want to do a find
within a specific range, an redefine the range as until I reach the end of
the document. I was able to specify a specific range using the code from an
earlier post (1/24/06), but I cannot do a find once that range is defind. I
used the following to define the range

Dim oRngStart As Range
Dim oRngEnd As Range
Dim oRngDefined As Range
With Selection
..HomeKey Unit:=wdStory
With .Find
..ClearFormatting
..Text = "xxxxxx"
..Execute

Set oRngStart = Selection.Range
Selection.MoveRight

..Text = "yyyyy"
..Execute
Set oRngEnd = Selection.Range
End With
End With

Set oRngDefined = ActiveDocument.Range(Start:=oRngStart.End,
End:=oRngEnd.Start)

I then use

with oRngDefined.find
.Text="texttofind"
.Execute
End with

But the find oeration moved beyond the "oRngDefined" range when searching
for "texttofind".
 
T

Tony Jollans

A couple of points.

Firstly are you sure that the first two Finds are successful (as the code
doesn't check)?

Secondly, if the Find Range (oRngDefined) happens to equal the Find Text
exactly (texttofind), Find will not consider that to be part of the Range to
search (assuming it to have been the result of the previous Find,
presumably) and will start searching forward from the end of the Find Range.
 
P

prs16001

Tony,

The first 2 finds were successfull. I checked this by adding
"select.oRngDefine" after defining it and the range I wanted was highlighted.
I'm at the early stages of creating this macro and will need to do a check
to see if the finds are successfull.

The range of oRngDefined is very large (4 pages, 852 words, 5294
characters), and I want to find text within that, so the third find (e.g.
..text="texttofind") is a subset of the defined range and is contained within
that range. The overall document is large (200X the above defined range) and
the start and end markers I'm interested in are repeated multiple times with
different information contained within that range. I want to find the
information between those ranges.

thanks
 
H

Helmut Weber

Hi,

how about this one:

Sub Macro6000()
Dim rDcm As Range
Dim rTmp As Range
Set rDcm = ActiveDocument.Range
Set rTmp = Selection.Range
With rDcm.Find
.Text = "Landmark1*Landmark2"
.MatchWildcards = True
If .Execute Then
rTmp.SetRange Start:=rDcm.Start, End:=rDcm.End
With rTmp.Find
.Text = "quick"
.Replacement.Text = "quick"
.Replacement.Font.Color = wdColorRed
.Wrap = wdFindStop
.Execute Replace:=wdReplaceAll
End With
End If
End With
End Sub


Need further clarification, ask again.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

Greg Maxey

Helmut,

or...
Sub Macro6000()
Dim oRngMajor As Range
Dim rRngMinor As Range
Set oRngMajor = ActiveDocument.Range
With oRngMajor.Find
.Text = "xxxxx*yyyyy"
.MatchWildcards = True
If .Execute Then
Set oRngMinor = oRngMajor.Duplicate
With oRngMinor.Find
.Text = "test"
.Replacement.Font.Color = wdColorRed
.Wrap = wdFindStop
.Execute Replace:=wdReplaceAll
End With
End If
End With
End Sub
 
H

Helmut Weber

Hi Submariner,

yes,

it's only that I never really understood
that duplicating of a range.

Maybe, I'll get it now.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

Greg Maxey

Helmut,

I'm the same way, but I always try it and if it works great, if not I
just stumble on in the dark.
 
P

prs16001

Thanks gentlemen, that is working for me now. I did have some trouble
initially since the first search I was using special characters (^w) and the
text contained the characters "[" and "]". But after some fumbling, I was
able to get it working using the wildcard.
 
P

prs16001

I now have the find working but not the way I need. I now have the subset of
text selected with your help. On the next find operation searching only
through the defined range, I want to select or extract text which is
different that the text in the find operation. As an example of the definded
range,

xxxxxx
anything
anything
texttofind this is what I really want
anything
anything
yyyyyyy

I want to do the find operation several times within the defined range. In
most circumstances,the text that I want is to the right of the text used the
find operation. I thought I could use

Selection.MoveRight unit:=wdCharacter, Count:=2
Selection.MoveRight unit:=wdSentence, Count:=1, Extend:=wdExtend
textiwant = Selection.Text

to select the text but that isn't working. I need to run the find operation
several times, each time looking for a different text string and selecting
the text to the right.
 
H

Helmut Weber

Hi,

working with ranges is sometimes tricky indeed.
When searching the activedocument.range, not like rDcm here,
but in numerous other examples to be found in this group,
rDcm shrinks to the find spot and expands again to the rest of the doc.
Sometimes one has to take care of all the shrinking and expanding oneself.

But it's worth while getting used to it.

Stay away from the selection.

One more example, without being able to discuss all aspects
and all possible complications.


Dim p1 As Long
Dim rDcm As Range
Dim rTmp As Range
Set rDcm = ActiveDocument.Range
Set rTmp = Selection.Range
With rDcm.Find
.Text = "Landmark1*Landmark2"
.MatchWildcards = True
If .Execute Then
Set rTmp = rDcm.Duplicate
p1 = rTmp.End
With rTmp.Find
.Text = "quer"
While .Execute
MsgBox rTmp.Characters.Last.Next.Sentences(1)
rTmp.Collapse direction:=wdCollapseEnd
rTmp.End = p1
Wend
End With
End If
End With
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