Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Selection.MoveStartUntil hangs on comment

112 views
Skip to first unread message

G-Money

unread,
Nov 29, 2007, 12:36:43 PM11/29/07
to
I am using the following snippet to search for numbers in a doc and
then select the text ahead of the number.

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "[0-9]*[?., ]"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = True
.MatchWildcards = True
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
While Selection.Find.Execute
MyNumber = Selection.Text
...
Selection.MoveStartUntil Cset:="!?." & Chr(10) & Chr(11) &
Chr(12) & Chr(13), Count:=wdBackward
...
Wend

The MoveStartUntil hangs forever if there is a comment in the text the
Selection is expanded backward to. Anybody have any ideas why this
happens or how I could fix it?

Thanks much.
Garrett

Tony Strazzeri

unread,
Jan 4, 2008, 7:34:45 PM1/4/08
to
Hi Garret,

How are you going with this?

Can you post a sample (text only) of the doc you are searching?

I made up some text to run your code over.

he code as it stands running on my text sample will get stuck ina loop
anyway. The reason is that the Selection.MoveStartUntil method you
are using moves the selection backwards. The net effect is that the
next iteration of the find will hit the very same target you just
moved back from.

Hope this helps.

Cheers
TonyS.

klaus...@gmail.com

unread,
Feb 7, 2008, 1:18:59 AM2/7/08
to
The comment marker has the ASCII code 5, so adding Chr(5) to CSet
should do
the trick.

Regards,
Klaus

G-Money

unread,
Feb 28, 2008, 10:41:27 AM2/28/08
to
Thanks Klaus and Tony.

Tony,

I left out a step before the MoveStartUntil:

SelectEnd = Selection.End

And a step after MoveStartUntil where I advance the selection past the
end of the previous selection:

Selection.MoveRight Unit:=wdCharacter, Count:=SelectEnd -
Selection.End, Extend:=wdMove
Selection.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdMove

Klaus,

Thanks for the Chr(5) info. Do you have any ideas about how to skip
over the comment data? Just adding Chr(5) to CSet would make me stop
there and potentially miss words before the comment that I want to
process. Is there a Chr(5) at the beginning of the comment as well?
I suppose I could get off my butt and take a look myself...

Below is a sample of the text I am searching. You need to add a Word
comment between the article "the" and the number "100" on the word
"test" to reproduce the problem. So, I do not want to stop at the
comment char because then I would miss "This is the test" when the
selection on "100" is expanded back to the previous period at the end
of the first sentence.

This is a test. This is the test 100.

Thanks again guys.

klaus...@gmail.com

unread,
Mar 3, 2008, 6:44:46 PM3/3/08
to
Hi Garrett,

I think Tony was on the money... The comments may not have much to do
with
the problem.
If you single-step through your code (F8 in the VBA editor), it
shouldn't be
too hard to find where the selection doesn't move as you expect?

At least I can't see anything in your code that would make it hang on
a
comment.

The comment marker (ASCII 5) might be a problem in the text you
retrieve,
and you may want to remove it:
myNumber = Replace(Selection.Text, ChrW(5), "")

Only BTW: There isn't anything to mark the start of the commented
text...
You'd need to look at myComment.Scope to tell the range that's
highlighted
("scoped").

Regards,
Klaus

"G-Money" <garrett....@gmail.com> schrieb:

0 new messages