MS Office Forum / Word / Programming / October 2007
how to select multiple discontinuous blocks of text with VBA
|
|
Thread rating:  |
Chip Orange - 31 Aug 2007 16:21 GMT It's possible to use the control key when selecting text with the mouse to have multiple areas of selected text that aren't continuous with one another. I need to do this with VBA.
I've tried recording a macro to see how it's done, but that didn't work.
I've tried defines range for each block I need selected, but as I use the range.select method on each successive block of text, it unselects the previous block.
Can anyone tell me how this can be done?
Thanks.
Chip
Jay Freedman - 31 Aug 2007 17:47 GMT It can't be done in VBA, because the programming was never done to support it: http://support.microsoft.com/?kbid=288424
You have to handle the blocks separately and sequentially.
-- 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.
>It's possible to use the control key when selecting text with the mouse to >have multiple areas of selected text that aren't continuous with one [quoted text clipped - 12 lines] > >Chip Chip Orange - 31 Aug 2007 18:34 GMT Thanks Jay; I wouldn't have thought to just give up!!! I can't handle them separately, as I need to present them all at once to the user, in the source document, for approval or modification, before I process them.
I've never done this before, but what if I opened multiple window views on the source document, could I then have a block of selected text in each window?
Thanks.
Chip
> It can't be done in VBA, because the programming was never done to > support it: http://support.microsoft.com/?kbid=288424 [quoted text clipped - 24 lines] >> >>Chip Jay Freedman - 31 Aug 2007 19:17 GMT It looks like multiple window views can have separate selections, at least in the UI, but I haven't tried to set that up in a macro. Seems to me it would be a really sucky way to show it to a user unless there were at most three or four blocks.
Have you thought of using highlighting or shading to indicate the changed areas, which the macro could remove after the user has approved or modified the changes?
-- 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.
>Thanks Jay; I wouldn't have thought to just give up!!! I can't handle them >separately, as I need to present them all at once to the user, in the source [quoted text clipped - 36 lines] >>> >>>Chip Chip Orange - 31 Aug 2007 19:34 GMT > It looks like multiple window views can have separate selections, at > least in the UI, but I haven't tried to set that up in a macro. Seems > to me it would be a really sucky way to show it to a user unless there > were at most three or four blocks. Yah, you're probably right about that.
> Have you thought of using highlighting or shading to indicate the > changed areas, which the macro could remove after the user has > approved or modified the changes? I like that, but if changes were need (expanding or shrinking the "selected" block of text), it would be hard for the user to do that way ... still, let me think about that.
Thanks again,
Chip
Helmut Weber - 31 Aug 2007 21:47 GMT Hi Chip,
shrink highlighting to the selection:
Sub Test612() Dim rTmp As Range Set rTmp = Selection.Range With rTmp.Characters While .First.Previous.HighlightColorIndex <> wdNoHighlight .First.Previous.HighlightColorIndex = wdNoHighlight rTmp.Start = rTmp.Start - 1 Wend While .Last.Next.HighlightColorIndex <> wdNoHighlight .Last.Next.HighlightColorIndex = wdNoHighlight rTmp.End = rTmp.End + 1 Wend End With End Sub
 Signature Greetings from Bavaria, Germany
Helmut Weber, MVP WordVBA
Win XP, Office 2003 "red.sys" & Chr$(64) & "t-online.de"
Chip Orange - 04 Sep 2007 18:37 GMT Thanks Helmut.
If I understand this, and I'm not sure I do, I think it's not exactly what I need, but it gives me an idea of how to get there.
I need to present to the user a document, with multiple discontiguous blocks of text highlighted, and let them change the highlighting (which is serving as a substitute for selecting). When they're done they can signal to my program in some way, and I'll process the highlighted text as if it had been selected (that is, I'll copy it all out and do something with it).
They're used to selecting the text to be processed, but they complain it's too time-consuming, and have asked me to pre-select the correct text for them to approve with just a click. I would have to get them used to highlighting instead of selecting.
Thanks for the idea.
Chip
> Hi Chip, > [quoted text clipped - 14 lines] > End With > End Sub Helmut Weber - 04 Sep 2007 20:34 GMT Hi Chip,
interesting.
The following should remove highlighting from an highlighited range of text, if the cursor is the insertion point and within the range.
If the cursor is extended, it will reduce surrounding highlighting to the selection.
Sub SetHighlight() Dim rTmp As Range Set rTmp = Selection.Range Selection.Range.HighlightColorIndex = wdYellow With rTmp.Characters While .First.Previous.HighlightColorIndex <> wdNoHighlight .First.Previous.HighlightColorIndex = wdNoHighlight rTmp.Start = rTmp.Start - 1 Wend While .Last.Next.HighlightColorIndex <> wdNoHighlight .Last.Next.HighlightColorIndex = wdNoHighlight rTmp.End = rTmp.End + 1 Wend End With End Sub
But it is slow.
And there is: "Tools, Options, Edit, When selecting, automatically select entire word", which should be unchecked.
One could speed it up a lot, if your docs are large and your users are, so to speak, not compliant, by checking whether the selection is "inrange" with a highlighted range it touches.
But, IMHO, thats overdoing it.
 Signature Greetings from Bavaria, Germany
Helmut Weber, MVP WordVBA
Win XP, Office 2003 "red.sys" & Chr$(64) & "t-online.de"
Russ - 19 Sep 2007 06:42 GMT Chip, Another thing you could do is add two macros to the user's contextual menu. Also called shortcut menu for Text, when right clicking in a main text body, using the Tools/Customize menu. When adding macros to menu lists, you can name the menuitem text whatever you want and create shortcut key combinations to activate macros. One right click menu item macro would be added for highlighting selected text and another would be added to remove the highlighting from selected text. (Also remind user of the Undo and Redo buttons) Editing should be swift even on your pre-suggested highlighted text and you can still use the discontinuous selection method for editing/(un)highlighting selections.
> Thanks Helmut. > [quoted text clipped - 34 lines] >> End With >> End Sub
 Signature Russ
drsmN0SPAMikleAThotmailD0Tcom.INVALID
Russ - 19 Sep 2007 06:54 GMT Chip, The two macros could be basically one-liners, such as:
Selection.Range.HighlightColorIndex = wdTeal and Selection.Range.HighlightColorIndex = wdNoHighlight
> Chip, > Another thing you could do is add two macros to the user's contextual menu. [quoted text clipped - 46 lines] >>> End With >>> End Sub
 Signature Russ
drsmN0SPAMikleAThotmailD0Tcom.INVALID
Chip Orange - 20 Sep 2007 14:31 GMT Thanks Rus, but I completed the project before I saw this suggestion. I will look into contextual item macros, as I didn't know this was possible, and it would come in very handy in some situations.
In this one however, avoiding the use of selecting discontinuous text by the user was the goal, as it's easy to lose all your selections by clicking without the control key just once! :(
Having them highlight, and then my processing all the highlighted text when they're done is apparently working well, and less prone to error.
Regards,
Chip
> Chip, > Another thing you could do is add two macros to the user's contextual [quoted text clipped - 54 lines] >>> End With >>> End Sub Russ - 20 Sep 2007 18:07 GMT Chip, Selecting text and highlighting the selected text with a right click menu option, I thought would be the fastest way to highlight a bunch of separate text, without messing with their current format font options. You could also show the user how to use the Format Painter Icon in the standard toolbar. If you are not familiar with the Format Painter, use the regular Word Help system and search for the word 'painter'. Double clicking the icon causes it to become 'sticky'.
> Thanks Rus, but I completed the project before I saw this suggestion. I > will look into contextual item macros, as I didn't know this was possible, [quoted text clipped - 69 lines] >>>> End With >>>> End Sub
 Signature Russ
drsmN0SPAMikleAThotmailD0Tcom.INVALID
Chip Orange - 21 Sep 2007 19:25 GMT Hi Rus,
You're right about what's fastest. My problem, unless I'm mis-understanding you, is that they could spend 10 minutes selecting just the right text in a very large document, and then accidentally click or do something that loses all that selection. They get *very* perterbed when that happens, and they asked me for a way both for my program to pre-indicate the text to be processed (highlighted or selected), and to make it more "fool proof" so that one wrong click won't cause a loss of all work thusfar. That's why, since I can't pre-select distinct blocks of text, and since selections can so easily be lost, I'm avoiding use of selected text (except they can select as they go I suposed and highlight using the contextual macros you suggest, which could save them some clicks, but I'm hoping my preprocessing will save them even more if I can always guess right).
Thanks again.
I'll go look at the painter.
Chip
> Chip, > Selecting text and highlighting the selected text with a right click menu [quoted text clipped - 86 lines] >>>>> End With >>>>> End Sub Russ - 23 Sep 2007 03:08 GMT Chip, By highlight, of course I mean changing the background color of text to a highlight color (green, yellow, etc.).
I was just offering a fast way for either you or them to select and (un)highlight batches of text. They don't need to do it all with one big selection. Select some and (un)highlight, select more and (un)highlight, etc. That's the normal process anyone uses to highlight text manually. The right click menu choices or hotkey combinations just offer a faster way to do the (un)highlighting part of the process.
When everything wanted is highlighted, then you process the highlighted text.
You can pre-indicate text for them and the right click menu method still offers a quick way for them to edit the pre-indicated text.
> Hi Rus, > [quoted text clipped - 107 lines] >>>>>> End With >>>>>> End Sub
 Signature Russ
drsmN0SPAMikleAThotmailD0Tcom.INVALID
Chip Orange - 25 Sep 2007 18:36 GMT Yes, of course Rus; sorry if I confused things.
I will add those context menu shortcuts as soon as I lookup how to do so, as I agree with you, it will be of even more help to them. I've been working lately on pre-selecting/highlighting just the right text (this is a problem for inexperienced employees).
Thanks again,
Chip
> Chip, > By highlight, of course I mean changing the background color of text to a [quoted text clipped - 142 lines] >>>>>>> End With >>>>>>> End Sub Russ - 26 Sep 2007 19:06 GMT Chip, If there are very specific patterns to what you want highlighted all the time, you can create macros to do the work for you. Afterwards, double check what the macros found and amend that.
For example, I use at work what is called G-Code to program metal cutting machines. I created a macro that colors G-Code syntax commands and variables and uses other things in the code, including spacing, to make the code 'pretty' or easier to read. It 'filters' through the raw code to create a pretty copy for human reading.
I'm just suggesting to use macros to help you pre-highlight certain patterns.
> Yes, of course Rus; sorry if I confused things. > [quoted text clipped - 153 lines] >>>>>>>> End With >>>>>>>> End Sub
 Signature Russ
drsmN0SPAMikleAThotmailD0Tcom.INVALID
Chip Orange - 05 Oct 2007 17:06 GMT Thanks Rus,
That's exactly what I'm doing; highlighting the pre-selected text with macros, allowing them to change the highlighting, and when they close it, intercepting the event and copying the highlighted text into another document, formatting it, and placing it into the proper locations, adding hyperlinks, etc.
They're much happier with the arrangement of highlighting the text rather than my previous arrangement of having them select disjoint blocks of text for copying. It's much more obvious what's going to be copied, and it's less likely a mistake will be made.
Regards,
Chip
> Chip, > If there are very specific patterns to what you want highlighted all the [quoted text clipped - 189 lines] >>>>>>>>> End With >>>>>>>>> End Sub Russ - 31 Aug 2007 19:19 GMT Chip, Something I stumbled upon the other day is that the "Spike" is a special part of AutoText for separated text and can be manipulated through VBA.
Look up the word Spike in regular Word Help for use with keystrokes and in Word VBA Help for 'appendtospike' examples.
> Thanks Jay; I wouldn't have thought to just give up!!! I can't handle them > separately, as I need to present them all at once to the user, in the source [quoted text clipped - 36 lines] >>> >>> Chip
 Signature Russ
drsmN0SPAMikleAThotmailD0Tcom.INVALID
Chip Orange - 31 Aug 2007 20:35 GMT Thanks for the response Russ, but I don't need to just collect the text, I need it displayed, selected, but in it's original context in the source document. I don't think the spike helps me with anything other than concatenating the text into a single place.
Thanks anyway,
Chip
> Chip, > Something I stumbled upon the other day is that the "Spike" is a special [quoted text clipped - 49 lines] >>>> >>>> Chip
|
|
|