MS Office Forum / Word / Programming / February 2008
Selecting Specific Range Within A Document Without Search (Word 2003)
|
|
Thread rating:  |
Alan Stancliff - 27 Feb 2008 23:22 GMT 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
Tony Jollans - 28 Feb 2008 00:00 GMT 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.
 Signature Enjoy, Tony
> Here's a Word 2003 VBA question about RANGEs that's been driving me nuts > for a while. [quoted text clipped - 24 lines] > > Alan Alan Stancliff - 28 Feb 2008 00:42 GMT 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").
> Without using (built-in) search functionality you will have to write > your own - checking each paragraph (using any algorithm you like) until [quoted text clipped - 3 lines] > within it using Selection.MoveUntil and Selection.MoveEndUntil and other > similar methods. Tony Jollans - 28 Feb 2008 01:19 GMT 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?
 Signature Enjoy, Tony
> Thanks Tony, > [quoted text clipped - 48 lines] >> within it using Selection.MoveUntil and Selection.MoveEndUntil and other >> similar methods. Greg Maxey - 28 Feb 2008 02:44 GMT 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
 Signature ~~~~~~~~~~~~~~~~~~~~~~~~~~~ Greg Maxey - Word MVP
My web site http://gregmaxey.mvps.org Word MVP web site http://word.mvps.org ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> Thanks Tony, > [quoted text clipped - 48 lines] >> selection within it using Selection.MoveUntil and >> Selection.MoveEndUntil and other similar methods. Jay Freedman - 28 Feb 2008 01:18 GMT 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.
>Without using (built-in) search functionality you will have to write your >own - checking each paragraph (using any algorithm you like) until you [quoted text clipped - 32 lines] >> >> 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.
Alan Stancliff - 28 Feb 2008 01:28 GMT 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_eb67b 351-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
> 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 [quoted text clipped - 42 lines] > Microsoft Word MVP FAQ: http://word.mvps.org > Email cannot be acknowledged; please post all follow-ups to the newsgroup so all may benefit. Jay Freedman - 28 Feb 2008 03:43 GMT 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.
>Hi Jay, > [quoted text clipped - 59 lines] >> 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.
Alan Stancliff - 28 Feb 2008 08:32 GMT 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
****************************************
> Hi Alan, > [quoted text clipped - 71 lines] > Microsoft Word MVP FAQ: http://word.mvps.org > Email cannot be acknowledged; please post all follow-ups to the newsgroup so all may benefit. Doug Robbins - Word MVP - 28 Feb 2008 09:29 GMT 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
 Signature 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
> Hi Jay, > [quoted text clipped - 204 lines] >> Email cannot be acknowledged; please post all follow-ups to the newsgroup >> so all may benefit. Alan Stancliff - 28 Feb 2008 10:48 GMT 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
> For the first one use: > [quoted text clipped - 13 lines] > myrange.End = myrange.start + InStr(myrange, ",") - 1 > MsgBox myrange.Text Doug Robbins - Word MVP - 28 Feb 2008 19:25 GMT Set myrange = ActiveDocument.Sections(1).Range
 Signature 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
> Doug, > [quoted text clipped - 28 lines] >> myrange.End = myrange.start + InStr(myrange, ",") - 1 >> MsgBox myrange.Text Alan Stancliff - 28 Feb 2008 20:31 GMT > Set myrange = ActiveDocument.Sections(1).Range Thanks, Doug.
I appreciate your help.
Alan Stancliff - 28 Feb 2008 21:25 GMT 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
> For the first one use: > [quoted text clipped - 13 lines] > myrange.End = myrange.start + InStr(myrange, ",") - 1 > MsgBox myrange.Text Doug Robbins - Word MVP - 28 Feb 2008 22:20 GMT 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
 Signature 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
> Hi Doug (or anybody else willing to help) > [quoted text clipped - 43 lines] >> myrange.End = myrange.start + InStr(myrange, ",") - 1 >> MsgBox myrange.Text Alan Stancliff - 29 Feb 2008 02:50 GMT 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
> 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 [quoted text clipped - 7 lines] > myrange.End = myrange.Paragraphs(1).Range.End > MsgBox myrange.Text Doug Robbins - Word MVP - 29 Feb 2008 04:10 GMT myrange.End = myrange.Paragraphs(1).Range.End - 1
 Signature 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
> Thanks Doug, > [quoted text clipped - 24 lines] >> myrange.End = myrange.Paragraphs(1).Range.End >> MsgBox myrange.Text Alan Stancliff - 29 Feb 2008 08:54 GMT > 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
Tony Jollans - 28 Feb 2008 10:30 GMT 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.
 Signature Enjoy, Tony
> Hi Jay, > [quoted text clipped - 204 lines] >> Email cannot be acknowledged; please post all follow-ups to the newsgroup >> so all may benefit.
|
|
|