Group: microsoft.public.word.vba.general
From: "Greg Maxey"
Date: Wednesday, February 27, 2008 8:44 PM
Subject: Re: Selecting Specific Range Within A Document Without Search (Word 2003)

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



Alan Stancliff wrote:
> 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").
>
>
> Tony Jollans wrote:
>> 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.


Safety Articles | Usenet Groups | Usenet News | Bluegrass