Selecting Specific Range Within A Document Without Search (Word 2003)

A

Alan Stancliff

Here's a Word 2003 VBA question about RANGEs that's been driving me nuts
for a while.

Supposing I have a document that somewhere has the following on a line
by itself, effectively being a paragraph:

**********
(A) MY SAMPLE WORDS TO SELECT: Hello World

and later on it has a line that has

(B) MY OTHER SAMPLE WORDS TO SELECT: Hello Universe, So Vast Thou Art.
*********

Without using the search functionality, how would one code to do the
following three tasks:
1. Select the the stuff following the colon and two spaces in sample
(A), whatever it may be (in this case, "Hello World")

2. Select the stuff before the comma in my example (B), whatever it may
be (in this case it is Hello Universe)

3 Select the stuff between the comma and end of the line (in this case,
it is "So Vast Thou Art").


Regards,

Alan
 
T

Tony Jollans

Without using (built-in) search functionality you will have to write your
own - checking each paragraph (using any algorithm you like) until you
identify the one you want.

Having identified the paragraph you can scan it and move the selection
within it using Selection.MoveUntil and Selection.MoveEndUntil and other
similar methods.
 
A

Alan Stancliff

Thanks Tony,

I looked at the help file for Selection.MoveUntil and
Selection.MoveEndUntil.

Unfortunately, I'm pretty much of a beginner at VBA, and I'm not sure
what algorithm to use or how to use it. As you might guess, my question
was a generalization that I devised to help me learn something to help
write a macro. Mostly, what I have been doing is recording and editing
the recorded stuff, while trying to read up on how VBA works.

Do you think it might be possible to give me a few snippits of code that
would do the three tasks I asked about. I would be most grateful.

For anyone looking at this, below my signature line, I am restating the
question in my original post in case you can't see it

Regards,

Alan Stancliff

++++++QUESTION++POSED++++++++++

Supposing I have a document that somewhere has the following on a line
by itself, effectively being a paragraph:

**********
(A) MY SAMPLE WORDS TO SELECT: Hello World

and later on it has a line that has

(B) MY OTHER SAMPLE WORDS TO SELECT: Hello Universe, So Vast Thou Art.
*********

Without using the search functionality, how would one code to do the
following three tasks:
1. Select the the stuff following the colon and two spaces in sample
(A), whatever it may be (in this case, "Hello World")

2. Select the stuff before the comma in my example (B), whatever it may
be (in this case it is Hello Universe)

3 Select the stuff between the comma and end of the line (in this case,
it is "So Vast Thou Art").
 
J

Jay Freedman

My 2 cents... While Tony's answer is correct, using the search function is MUCH
faster and easier than any other method. If you can think of a good reason not
to use it, I'd like to hear about it.
 
T

Tony Jollans

Hi Alan,

Assuming your cursor is in the right paragraph to begin with (which you'll
have to do separately) then

Selection.Paragraphs(1).Range.Select
Selection.Collapse wdCollapseStart
Selection.MoveUntil ":"
Selection.MoveRight , 3
Selection.MoveEndUntil Chr(13)

will select the paragraph, fall back to the beginning, roll forward to the
colon, then a couple spaces then stretch out to the end.

Of course you would be better not using the selection but that's for another
day :)

Does that help?
 
G

Greg Maxey

I don't know. Something like this:

Sub ScratchmacroI()
Dim oPara As Paragraph
For Each oPara In ActiveDocument.Range.Paragraphs
If Left(oPara.Range.Text, Len(oPara.Range.Text) - 1) Like "MY SAMPLE WORDS
TO SELECT: Hello World" Then
oPara.Range.Select
With Selection
.MoveStartUntil ":", wdForward
.MoveStart wdCharacter, 3
Exit Sub
End With
End If
Next
End Sub
Sub ScratchmacroII()
Dim oPara As Paragraph
For Each oPara In ActiveDocument.Range.Paragraphs
If Left(oPara.Range.Text, Len(oPara.Range.Text) - 1) Like "MY OTHER SAMPLE
WORDS TO SELECT: Hello Universe, So Vast Thou Art." Then
oPara.Range.Select
With Selection
.MoveStartUntil ":", wdForward
.MoveStart wdCharacter, 3
.MoveEndUntil ",", wdBackward
.MoveEnd wdCharacter, -1
Exit Sub
End With
End If
Next
End Sub
Sub ScratchmacroIII()
Dim oPara As Paragraph
For Each oPara In ActiveDocument.Range.Paragraphs
If Left(oPara.Range.Text, Len(oPara.Range.Text) - 1) Like "MY OTHER SAMPLE
WORDS TO SELECT: Hello Universe, So Vast Thou Art." Then
oPara.Range.Select
With Selection
.Collapse wdCollapseEnd
.MoveStartUntil ",", wdBackward
.MoveStart wdCharacter, 1
Exit Sub
End With
End If
Next
End Sub


--
~~~~~~~~~~~~~~~~~~~~~~~~~~~
Greg Maxey - Word MVP

My web site http://gregmaxey.mvps.org
Word MVP web site http://word.mvps.org~~~~~~~~~~~~~~~~~~~~~~~~~~
 
J

Jay Freedman

Hi Alan,

If you use a Range object instead of the Selection, as in the demo() sample in
the posts you pointed to, then the screen won't jump at all. The problem you're
having is largely that the macro recorder always uses the Selection -- one of
its more serious drawbacks.
 
A

Alan Stancliff

Hi Jay,

You wrote:
*********
If you use a Range object instead of the Selection, as in the demo()
sample in
the posts you pointed to, then the screen won't jump at all. The problem
you're
having is largely that the macro recorder always uses the Selection --
one of
its more serious drawbacks.
*******

I've been spending several hours, actually much more than that if I
count the time I've spent during the last month trying to conceptualize
the problem, formulate it specifically, pose it intelligently, look at
the answers carefully, and experiment and research. In studying previous
posts, especially the one with your demo routine, I have come up against
a rather frustrating brick wall. I hope I can get this resolved as my
wife's patience is wearing thin.

I decided to record the tasks and then substitute the Range Object for
the Selection Object.

Here's how I can do the first task with a recorded macro:

Sub Eraseme()
Selection.Find.ClearFormatting
With Selection.Find
.Text = "MY SAMPLE WORDS TO SELECT:"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.MoveRight Unit:=wdWord, Count:=1
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
End Sub



Here is my latest of 14 gazillion attempts to use Range Object instead
of the Selection Object. It falls apart and I don't know why.

Sub Eraseme()
Dim oRg As Range
Set oRg = ActiveDocument.Range
With oRg.Find
.Text = "MY SAMPLE WORDS TO SELECT:"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
range.find.execute
Selection.MoveRight Unit:=wdWord, Count:=1
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
End Sub

Here is how I could accomplish the second sample question with a
recorded macro. This throws in the element of wildcards. How could I
substitute the use of the Range Object instead?

Sub eraseme()

Selection.Find.ClearFormatting
With Selection.Find
.Text = "MY OTHER SAMPLE WORDS TO SELECT:"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.MoveRight Unit:=wdWord, Count:=1
Selection.Find.ClearFormatting
With Selection.Find
.Text = "*,"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.Find.Execute
End Sub

My purpose is to assign the selected items to either an Autocorrect
Entries or a variables in several macros.

Please, someone just show me how to substitute the Range Object for the
Selection Object in these. I really would be most grateful for the help.
Moreover, it would really help me understand more about VBA.

Regards,

Alan Stancliff

****************************************
 
D

Doug Robbins - Word MVP

For the first one use:

Dim myrange As Range
Set myrange = ActiveDocument.Range
myrange.start = myrange.start + InStr(myrange, "MY SAMPLE WORDS TO
SELECT:") + 26
myrange.End = myrange.Paragraphs(1).Range.End
MsgBox myrange.Text

and for the second

Dim myrange As Range
Set myrange = ActiveDocument.Range
myrange.start = myrange.start + InStr(myrange, "MY OTHER SAMPLE WORDS
TO SELECT") + 32
myrange.End = myrange.start + InStr(myrange, ",") - 1
MsgBox myrange.Text


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

1. You set up the Find object belonging to the oRg object. You must then
execute the same Find object, so ...

oRg.Find.execute

Having done that you must then manipulate oRg instead of Selection.

2. Again, set up a range and then substitute it for Selection.
 
A

Alan Stancliff

Doug,

Thank you so much. Those worked exactly. I will now look up everything
about them and then apply that to my real-world problem.

By the way, If I wanted to set myrange to continuous section 1 in a
3-section document, how would I do that? I have tried this:
Set myrange = ActiveDocument.Range.Sections(1)

But I get a run-time error 13, type mismatch.

Regards,

Alan Stancliff
 
D

Doug Robbins - Word MVP

Set myrange = ActiveDocument.Sections(1).Range

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

Alan Stancliff

Hi Doug (or anybody else willing to help)

You gave me this bit of code:

********
Sub Testme()
Dim myrange As Range
Set myrange = ActiveDocument.Sections(1).Range
myrange.start = myrange.start + InStr(myrange, "MY OTHER SAMPLE
WORDS TO SELECT") + 32
myrange.End = myrange.start + InStr(myrange, ",") - 1
MsgBox myrange.Text
End Sub
**********

which selects the stuff between the end of the string "MY OTHER SAMPLE
WORDS TO SELECT" and a comma in a sentence that might look like this:
(B) MY OTHER SAMPLE WORDS TO SELECT: Hello Universe, So Vast Thou Art.

It works great!

But suppose that I wanted to select the stuff between the comma
following the string "MY OTHER SAMPLE WORDS TO SELECT" and the end of
the line (paragraph), and I don't know what's between the end of the
string and the paragraph mark?

Regards,

Alan Stancliff
 
D

Doug Robbins - Word MVP

You make use of the Instr() command to determine the amount by which the
..Start of the Range (myrange) must be used and then set the .End of the
Range to the .End of the Range of the Paragraph

Dim myrange As Range
Set myrange = ActiveDocument.Range
myrange.start = myrange.start + InStr(myrange, "MY OTHER SAMPLE WORDS
TO SELECT") + 32
myrange.Start = myrange.start + InStr(myrange, ",")
myrange.End = myrange.Paragraphs(1).Range.End
MsgBox myrange.Text

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

Alan Stancliff

Thanks Doug,

I'll be sure to play with that as soon as I get off work.

In the lines that say:
myrange.Start = myrange.start + InStr(myrange, ",")
myrange.End = myrange.Paragraphs(1).Range.End

That includes everything from the comma to the paragraph marker,
including the marker. How would I alter that second line to include
everything up to the paragraph marker but not the paragraph marker itself?

Regards,

Alan Stancliff
 
D

Doug Robbins - Word MVP

myrange.End = myrange.Paragraphs(1).Range.End - 1

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

Alan Stancliff

Doug said:
myrange.End = myrange.Paragraphs(1).Range.End - 1
Thanks again Doug,

I got a bunch of leads now. And thanks to Tony, Greg, and Jay too (I
hope I didn't forget someone).

Regards,

Alan Stancliff
 

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