searching a range

E

Elaine J.

Can someone tell me what is wrong with this code? What I am trying to do, is
set a range to the first 5 sections of a document. Then I want to find a
particular string. If I find it, I want to clear that search and search for
something different. However, the search is not working (at all). It is
not finding the string that is actually in the document. (I tried changing
noticeplus.find to selection.find, which appeared to work. But as I got
deeper into the macro, I found that it was not doing what I wanted (It wasn't
just searching the range that I set, it was searching the entire document).
I know I have done this before and I know it is going to be something simple
and stupid, but it just has me stumped at the moment. Thanks for any help.

Dim noticeplus as range
Selection.HomeKey Unit:=wdStory
Set noticeplus =
ActiveDocument.Range(Start:=ActiveDocument.Sections(1).Range.Start,
End:=ActiveDocument.Sections(5).Range.End)
With noticeplus.Find
.ClearFormatting
.Text = "Electronic"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
End With


If noticeplus.Find.Execute("Electronic") = True Then
Selection.Collapse 1

Selection.HomeKey Unit:=wdStory

Set noticeplus =
ActiveDocument.Range(Start:=ActiveDocument.Sections(1).Range.Start,
End:=ActiveDocument.Sections(5).Range.End)

With noticeplus.Find
.ClearFormatting
.Text = "in accordance with your contract"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
End With
'Expert if
If noticeplus.Find.Execute = True Then
Selection.Collapse 1
Selection.HomeKey Unit:=wdStory
 
G

Greg Maxey

I suppose I would use something like this:

Sub Test()
Dim noticeplus As Range
Dim oRng As Word.Range
Dim bFound As Boolean
Selection.HomeKey Unit:=wdStory
bFound = False
Set noticeplus = ActiveDocument.Range(Start:=0,
End:=ActiveDocument.Sections(5).Range.End)
Set oRng = noticeplus.Duplicate
With noticeplus.Find
.ClearFormatting
.Text = "Electronic"
.Forward = True
.Wrap = wdFindStop
.Execute
If .Found = True Then
bFound = True
End If
End With
If bFound Then
With oRng.Find
.Text = "in accordance with your contract"
.Forward = True
.Wrap = wdFindStop
.Execute
If .Found = True Then
'What do you want to do with this found text???
Selection.Collapse 1
Selection.HomeKey Unit:=wdStory
End If
End With
End If
End Sub
 
E

Elaine J.

Greg, thanks for your response. I think I am following most of that, but I
do have a question. What exactly is the the statement "Set oRng =
noticeplus.Duplicate" doing? It is creating an actual duplicate? What I am
trying to do is a little convulated (or at least I think it is).

I have a document that is comprised of many notices (with sections). Each
of the notices are three sections long, but some may be four and some five
sections long. It depends on language that is unique to each section.

So I am setting the range for 5 sections. If I find the language I am
looking for in section 5 -- that tells me something in particular. (and I
need to know that first). If I do find the language for section 5, then I
need to search to see if I find the langauge that would be in section 4.

If I find the right language in section 5, AND section 4, then I need to
delete section 4 and change my range to sections 1 to 4 (Sections 1-3 plus
section 5 that is now section 4)

After I determine the range, I will then print and delete that. Then I will
go through the same procedure for the next notice in the document.

By the way, if I don't find the language I am looking for in section 5, I
would immediately change my range to sections 1-4 -- and then if I don't find
the language in section 4 I would change it again to sections 1-3

I actually have most of it figured out, but for some reason the searching
within the range, is what has me stumped.

I'm saying all that to say that I need to be working in my orignal document.
I am not where I can test this until tomorrow, but I guess my original
question is pretty much what I need to know for right now. (And if you have
any other suggestions after reading this). I thought once I set a range, I
could just search it and do what I wanted to with it. It's turning out to be
more complicated than I thought.

Thanks again for your help.
 
G

Greg Maxey

Elaine,

Yes .Duplicate is just a exact copy of the specified range. You could just
as easily use:

Dim oRng1 as Word.Range
Dim oRng2 as Word.Range
Set oRng1 = ActiveDocument.range
Set oRng2 = ActiveDocument.range
 

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