trying to use MoveEndUntil to extend a selected range

J

joncohen

I am trying to use the MoveEndUntil to expand the selected range in this
code, however, the text "WORK" is on a different line then the text "MEDICAL
RECORDS:". When I debug this logic the selected range stops one word after
medical records on the second line. Not sure if I can use "MoveEndUntil" to
extend beyond a single sentence. The version of word is 2003, any help
would be appreciated.

Thx,

Jonathan

TxtStrg = "MEDICAL RECORDS:"

Set BMRange = ActiveDocument.Range

Set BMRngRsult = BMRange.Duplicate

Do

With BMRngRsult.Find
.ClearFormatting
.Text = TxtStrg
.Forward = True
.Wrap = wdFindStop
.Execute
End With

If Not BMRngRsult.Find.Found Then Exit Do

BMRngRsult.Select
BMRngRsult.Bold = wdToggle

'Selection
With BMRngRsult
.MoveEndUntil Cset:="WORK ", Count:=wdForward

' .MoveEnd Unit:=wdSentence, Count:=3
.Select
End With


BMRngRsult.MoveStart wdWord
BMRngRsult.End = BMRange.End

Loop Until Not BMRngRsult.Find.Found
 
D

Doug Robbins - Word MVP

Tell us what you really need to do, not how you are trying to do it.

--
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
 
T

Tony Jollans

MoveEndUntil works on the whole document but ....

... it doesn't do what you want.

It moves the end until one, any one, of the specified characters is found -
not the whole string. In your case I suspect it is stopping at the first
space.
 
J

joncohen

Tony,

Thank you ... that explains what I am seeing in the debugger. I looked at
the online help and this was not explained clearly. I take it that you can
not use this to find an entire string ... correct? Instead the find object
should be used to search for a full string?

- Jonathan
 
J

joncohen

Doug,

I am trying to select a block of text in a range to format the selected text
indentation. I was hoping that the MoveEndUntil would expand the original
selected range to include the new range of text up to the word "WORK ".
Tony's explaination of how the MoveEndUntil method works explains what I am
observing, it is stopping at the first space in the second line of text. Is
there a way to use an entire string with this method?

Thanks in advance.

- Jonathan
 
T

Tony Jollans

Yes, Jonathan, using Find would be the normal way. You may be able to use a
single wildcard Find rather than two separate ones - it rather depends on
the totality of what you are doing (as opposed to the simple extract you
posted)
 
D

Doug Robbins - Word MVP

The following code will select the block of text beginning with the M of
MEDICAL RECORDS and ending with the K of WORK

Dim arange As Range
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
.Execute FindText:="MEDICAL RECORDS:", MatchWildcards:=False,
Wrap:=wdFindStop, Forward:=True
End With
Set arange = Selection.Range
arange.End = ActiveDocument.Range.End
arange.End = arange.Start + InStr(arange, "WORK") + 3
arange.Select

--
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
 
R

Russ

Joncohen,
See below.
Yes, Jonathan, using Find would be the normal way. You may be able to use a
single wildcard Find rather than two separate ones - it rather depends on
the totality of what you are doing (as opposed to the simple extract you
posted)

MoveEndUntil only works on a set of characters, hence 'CSET', like Tony
said.
You could use find text "MEDICAL RECORDS:*WORK", using wildcards and
matchcase, if you knew that those phrases would be _unique_ in each record.
And then use the .expand method to include the whole sentence or paragraph.

Or if you knew that WORK was _always_, for example, 3 lines away from
MEDICAL RECORDS, you could use the .expand method to expand three lines the
selection or range in that direction.

Or use use 2 or 3 ranges; find MEDICAL RECORDS: and expand first range to
line or paragraph; and find next occurrence of WORK and expand second range
to line or paragraph; then make a range using one of the first two or a
third, with a starting point of the first found range and a ending point of
the second found range.

Searching with ranges can be tricky, because after each find you might have
to redefine the 'search range'; by collapsing to a new 'search range'
starting point and extending to a new 'search range' ending point, so that
you don't go into a endless loop.
 
J

joncohen

I would like to thank Doug, Tony and Russ for all their help with my
question. Everyone was very helpful. Doug, I ended up using your code
example to resolve my selecting a block of text in a range. I re-read the
vba on-line help for the MoveEndUntil method and it does say
"range or selection until any of the specified characters are found in the
document" . I did not realize that this ment only a single character match
.... now I know. Again thanks to everyone and I am very impressed with this
forum and this has been an invaluable resource.

Regards,

Jonathan
 

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