Doug
Thu Feb 28 01:29:15 PST 2008
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
"Alan Stancliff" <alanstancliff@nojunkyahoo.com> wrote in message
news:uSwGLSeeIHA.4140@TK2MSFTNGP04.phx.gbl...
>
> 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
>
> ****************************************
> Jay Freedman wrote:
>> 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. On Wed, 27 Feb 2008 17:28:04 -0800, Alan
>> Stancliff
>> <alanstancliff@nojunkyahoo.com> wrote:
>>
>>> Hi Jay,
>>>
>>> Yes, I was hoping to avoid the screen from jumping around during the
>>> performance of this task. I actually got the idea for the question by
>>> studying an exchange of messages we had last month, which you can refer
>>> to here:
>>>
>>> Browser link:
>>>
http://www.microsoft.com/communities/newsgroups/en-us/default.aspx?dg=microsoft.public.word.vba.general&tid=0e7336bb-59cf-4e33-99c8-c431abd8ef12&cat=en_US_eb67b351-9403-4888-a42c-b2b58efec0aa&lang=en&cr=US&sloc=en-us&m=1&p=1
>>>
>>> Newsreader link:
>>> news://msnews.microsoft.com:119/0E7336BB-59CF-4E33-99C8-C431ABD8EF12@microsoft.com
>>>
>>> Regards,
>>>
>>> Alan Stancliff
>>>
>>>
>>> Jay Freedman wrote:
>>>> 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.
>>>>
>>>>
>>>> On Thu, 28 Feb 2008 00:00:13 -0000, "Tony Jollans" <My forename at my
>>>> surname
>>>> dot com> 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.
>>>>>
>>>>> --
>>>>> Enjoy,
>>>>> Tony
>>>>>
>>>>> "Alan Stancliff" <alanstancliff@nojunkyahoo.com> wrote in message
>>>>> news:eDX4fgZeIHA.5400@TK2MSFTNGP04.phx.gbl...
>>>>>> 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
>>>> --
>>>> Regards,
>>>> Jay Freedman
>>>> Microsoft Word MVP FAQ:
http://word.mvps.org
>>>> Email cannot be acknowledged; please post all follow-ups to the
>>>> newsgroup so all may benefit.
>>
>> --
>> Regards,
>> Jay Freedman
>> Microsoft Word MVP FAQ:
http://word.mvps.org
>> Email cannot be acknowledged; please post all follow-ups to the newsgroup
>> so all may benefit.