Re: Move end of range to where I am? by Ed
Ed
Fri Feb 20 08:28:38 CST 2004
Thank you, Peter. That opens up a new world of possibilities to me!
Ed
"Peter Hewett" <Nospam@xtra.co.nz> wrote in message
news:Xns9496215438739Iwlpth@207.46.248.16...
> 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
>
>
> "Ed" <ed_millis@removethis.hotmail.com> wrote in
> news:u$qrQS79DHA.4084@tk2msftngp13.phx.gbl:
>
> > 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
> >
> > "Peter Hewett" <Nospam@xtra.co.nz> wrote in message
> > news:Xns949586B119DDCIwlpth@207.46.248.16...
> >> 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
> >>
> >>
> >> "Ed" <ed_millis@removethis.hotmail.com> wrote in
> >> news:OxWibox9DHA.3816@tk2msftngp13.phx.gbl:
> >>
> >> > 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?
> >> >
> >> >
> >> >
> >>
> >
> >
> >
>