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

Can more than a single character (i.e. considered as a string) be specified when using .MoveEndUntil to extend a range?

406 views
Skip to first unread message

Ian Jaffray

unread,
Mar 24, 2002, 7:25:02 PM3/24/02
to
Hello,

The Word 2002 VBA help on MoveEndUntil says:

"Moves the end position of the specified range or selection until any of the
specified characters are found in the document. If the movement is forward
in the document, the range or selection is expanded."

I'm trying to use .MoveEndUntil to extend the end of an existing range until
a sequence of 8 characters are found:

CharsMoved = myRange.MoveEndUntil(Cset:="<p><em>(", Count:=wdForward)

Does the Help explanation mean that you can load up any number of characters
into Cset, but any one of those characters suffices to trigger the end of
the .MoveEndUntil?

Although the sequence of characters I'm trying to test for does exist
shortly after the existing myRange in the document, these characters
considered as a string are not being "found".

If I change the Cset value to a single character (say, "p"), the Range is
extended as expected, to just before the next occurence of "p" in the
document.

If I change the Cset value to "em", the Range is extended to the character
before the next "e" in the document (which is not part of the hoped-for
sequence "em").

So, it does appear that any character of the variable Cset is being
utilized, whereas what I'd like to do is extend the Range until the string
"<p><em>(" is found.

Would anyone know a method for doing that?

Thanks very much for your help.

Ian Jaffray

Dave Rado

unread,
Mar 24, 2002, 7:43:57 PM3/24/02
to
Hi Ian

Simplest way (IMO) is covered at:
http://www.mvps.org/word/FAQs/MacrosVBA/SelectToFoundItem.htm

Regards

Dave


"Ian Jaffray" <ijaf...@sympatico.ca> wrote in message
news:F7un8.1033$mA1.2...@news20.bellglobal.com...

Ian Jaffray

unread,
Mar 24, 2002, 8:01:56 PM3/24/02
to
Dear Dave,

That looks straightforward, thanks very much. I'll check the mvps.org site
more extensively in the future.

Thank you.

Ian J.

"Dave Rado" <dr...@onetel.net.uk> wrote in message
news:eVatYY50BHA.2496@tkmsftngp04...

Klaus Linke

unread,
Mar 24, 2002, 8:48:58 PM3/24/02
to
"Ian Jaffray" <ijaf...@sympatico.ca> wrote:
> Hello,
>
> The Word 2002 VBA help on MoveEndUntil says:
>
> "Moves the end position of the specified range or selection
> until any of the specified characters are found in the document.
> If the movement is forward in the document, the range or
> selection is expanded."
>
> I'm trying to use .MoveEndUntil to extend the end of an
> existing range until a sequence of 8 characters are found:
>
> CharsMoved = myRange.MoveEndUntil(Cset:="<p><em>(",
Count:=wdForward)
>
> Does the Help explanation mean that you can load up any number
> of characters into Cset, but any one of those characters suffices to
> trigger the end of the .MoveEndUntil?


Yes, and it can be very useful -- but not for what you want to do :-)
Klaus


Ian Jaffray

unread,
Mar 25, 2002, 9:27:07 AM3/25/02
to
Dear Klaus,

> Yes, and it can be very useful -- but not for what you want to do :-)
> Klaus

Yes, you're right! What do you find it useful for? I guess I can see it
helping when looking for special characters like tabs or ... well, tabs. It
must not be meant especially for processing text characters (i.e.
alphanumeric).

Ian

Klaus Linke

unread,
Mar 25, 2002, 2:34:48 PM3/25/02
to
> Yes, you're right! What do you find it useful for? I guess I can see
it
> helping when looking for special characters like tabs or ... well,
tabs. It
> must not be meant especially for processing text characters (i.e.
> alphanumeric).

Hi Ian,

Tabs? Well, tabs or any other printing or non-printing character :-)
The nice thing about MoveStartWhile, MoveEndUntil ... is that you can
include any number of characters in CSet.

I often use it to extend or shrink ranges, for example to move the
end while there are numbers (Cset:="0123456789") or until there is
some cyrillic character, or to exclude paragraph marks and the
end-of-cell-marker from a selected table cell: Selection.MoveEndWhile
Cset:=Chr(13) & Chr(7), Count:=wdBackward.

If you want to do some serious parsing, string functions like Instr
are a lot faster, but as long as you want to keep working with the
Word Object Model (Ranges/Selection), they often are nice to work
with.

Greetings, Klaus


Ian Jaffray

unread,
Mar 25, 2002, 7:22:52 PM3/25/02
to
Dear Dave,

If you have a moment, could I ask if you might take a look at the code that
follows that is my attempt to use the ExtendMode technique you gave me the
link to on the mvps.org site?

I'm trying, starting with a range set to an initial paragraph (which is the
start of a question in a document), to then extend that range to the end of
the question (which could extend several paragraphs). Once that range has
been determined, I need to do some replacement of text within that range
(actually changing some HTML tags). This range, with the changed tags, would
then be copied into another document.

I'm blowing Word 2002 up when I attempt the second of the Find/Replace
operations on the established range (and I must admit I've been doing that
all day). I'm guessing that the Start and End of the range is being changed
during the Replace operation, as some characters are inserted. I don't
understand why that would cause the second Find/Replace to blow up, though.
If that has anything to do with what's happening, I would have thought the
range just wouldn't refer to the portion of the document I think it does,
but that the Find/Replace wouldn't have had the extreme consequences it
does.

Compounding this is my less than complete comfort working with range and
selection objects. I'd be grateful for any observations on your part as to
where I'm going wrong, or indeed, to a better approach. There's nothing
special about the document I'm running this on, it's a plain HTML file that
could as easily be a .TXT file. I know the question in this document starts
with paragraph 47 and that there are certain tags that are present in the
text, but otherwise, any plain text file could be substituted to run the
macro on, as long as it had more than 47 paragraphs and the correct
character string "<p><em>(" after the 47th paragraph to signal the end of
the question.

It's a lot to ask of you, but I'd appreciate any comments that might set me
aright.

Thank you,

Ian J.

The comments in the following code reflect what I think is happening, but at
least a few of them are probably incorrect.

-------------------------------------------------------
Option Explicit
Sub TestOfRangeAndSelection()

Dim myRange As Range

' Open a .htm file as a Plain Text document
Documents.Open FileName:="C:\Test\1994_0116.htm", ConfirmConversions:=False,
ReadOnly:=False, AddToRecentFiles:=False, PasswordDocument:="", _
PasswordTemplate:="", Revert:=False, WritePasswordDocument:="",
WritePasswordTemplate:="", Format:=wdOpenFormatText

' Create Range object that refers to a particular paragraph (the first
paragraph of a question in the document).
Set myRange = ActiveDocument.Paragraphs(47).Range

' ------------------
' Attempt to extend the Selection to find the combination of characters that
signals the end of the question.

' It's necessary to use a Selection for the recommended ExtendMode
operation, so create one from myRange.
myRange.Select

' Turn on ExtendMode
Selection.ExtendMode = True

' Perform the Find, looking for the string that signals that the end of the
question in the document has been reached.
With Selection.Find
.Text = "<p><em>("
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute
End With

' Turn off ExtendMode
Selection.ExtendMode = False

' ------------------
' Reduce the extended Selection by "<p><em>(" (i.e. the 8 characters that
signalled the end of the question).
Selection.MoveEnd Unit:=wdCharacter, Count:=-8

' Redefine the existing myRange from the current Selection. This should be
all the paragraphs of the question .
Set myRange = Selection.Range

' Within myRange, change some text.
With myRange.Find
.Forward = True
.Wrap = wdFindStop
.Text = "p>"
.Replacement.Text = "dd>"
.Execute Replace:=wdReplaceAll
End With

' Within the same myRange, change some more text.
' Here, when the .Execute statement is reached, Word locks up.
With myRange.Find
.Forward = True
.Wrap = wdFindStop
.Text = "dd>^p<dd"
.Replacement.Text = "dd>^p<dd>&nbsp;</dd>^p<dd"
.Execute Replace:=wdReplaceAll
End With

End Sub
-------------------------------------------------------


Dave Rado

unread,
Mar 27, 2002, 6:14:44 AM3/27/02
to
I've answered you duplicate post ("Any ideas on why Word 2002 blows up doing
Find/Replace in a range? ").

Regards

Dave


"Ian Jaffray" <ijaf...@sympatico.ca> wrote in message

news:vbPn8.2461$oz6.6...@news20.bellglobal.com...

Ian Jaffray

unread,
Mar 31, 2002, 8:41:45 AM3/31/02
to
Dear Dave,

Yes, thanks. I realized I was actually starting a new topic after I'd posted
that as a reply. So I reworded and posted anew. I was hoping you'd miss the
original. No such luck! I'll be a bit more careful in the future.

Thanks.

Ian J.

"Dave Rado" <dr...@onetel.net.uk> wrote in message

news:#QhZiEY1BHA.392@tkmsftngp03...

spiro...@gmail.com

unread,
Jun 26, 2020, 4:09:47 PM6/26/20
to
Oddly I have found this thread useful because the command acts exactly as you described, which is what I wanted it to do but thought from Microsoft's description that it did what you were trying to do. So now I know that I've found the correct code for what I want AND some useful links for if I want to find a string in future.

Nice when it works out that way, isn't it ;)

0 new messages