Move end of range to where I am?

E

Ed

I think I've got this right, but I'm not sure. I'm used to using Selection,
and I'm trying to do this in Range. I want to set a range that starts at
the beginning of the document and extends until the end of the paragraph
containing a specified text string. So far, I've got:
' Start at beginning of doc
rngTemp.SetRange(Start:=0, End:=1)
' Find end of range

With ActiveDocument.Range.Find

.Text = "^pFormNo. "

.Forward = True

.Execute

End With



Okay - that should put me where the Form No. is at the bottom of the range I
want. Except I need to extend this to the end of the paragraph, then reset
my rngTemp parameters from the beginning to that point.



First, I'm not sure I did the Find okay. I thought I read somewhere that
you can't mix Selection and Range, so I tried this with Range.



Second, with Selection, I have a visible cursor insertion point that makes
things easier. And I can MoveRight to the end of the paragraph. What am I
moving with Range and how do I move it?



Third, how do I return where I am so I can do another SetRange to include
everything from the beginning to where I am? Or is there a better way?
 
P

Peter Hewett

Hi Ed

Look at this it does what you want, you should be able to get your answers
by looking at this code:

Public Sub SelectFromStartOfDocToText()
Dim rngFind As Word.Range

' Setup find to search the entire document
Set rngFind = ActiveDocument.Content
With rngFind.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "^pFormNo. "
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = False

' Do find...
If .Execute Then

' Range object starts at the start of the document and
' extends to the end of the paragraph containing the
' found text
rngFind.Collapse wdCollapseEnd
rngFind.MoveEnd wdParagraph, 1
rngFind.Start = ActiveDocument.Range.Start

' Do something with your range here,
' you DONT need to select it!
rngFind.Select
Else
Set rngFind = Nothing
End If
End With

End Sub

When the code drops out of the loop the the rngFind object is Nothing if
the text was not found or points to a range from the start of your document
to the end of the paragraph containing the text that was searched for.

HTH + Cheers - Peter
 
E

Ed

Peter, it works like a charm! Thank you!

I'm still not too clear on how this works, though. You have rngFind set to
the entire document. In my limited knowledge, this means when you
CollapseEnd, you will wind up at the tail end of the doc. How did the end
point of rngFind get pulled back to the end of the Found text? Well, then
you set the Start to the document start, which is where I thought it would
already be. So then does the range reset itself around the found selection?

Ed
 
P

Peter Hewett

Hi Ed

In brief after the Find, if the execute method returns True then the range
object you used to define the range of the search is actually set to the
range of whatever it was you were searching for.

The easiest way to find out about this (anything in fact!) is to breakpoint
your code and explore using the Immediate and Local windows to determine
the values of the variables. You can then develop a hypothesis of what you
think is going on and then prove or disprove it!

Glad I was able to help + Cheers - Peter
 

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