Discontiguous selection in Word 2003

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 15621
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Discontiguous selection in Word 2003

Post by ChrisGreaves »

I’m having trouble understanding a discontiguous selection in Word 2003 and hope that someone might illuminate my mind.
Based on the MS article (please see P.S. below) I think that it is an almost-useless concept in Word 2003.

The attached short document has an index as an {Index} field.
Within the text generated by the {Index} I double-click to select the word “Select”.
I hold down the Ctrl key and double-click the word “Marking”.
I hold down the Ctrl key and double-click the word “Appends”.
Untitled.png
Then I run the short macro “test” supplied within the document’s Module1.
Everything I do to interrogate the Selection object results in only the last-selected word being considered.
ALMOST Everything I do to use the Selection object (“ShrinkDiscontiguousSelection” and “Copy”) delivers the three words – as I expect. The Print command outputs only the last-selected word!
Aaaaaargh!!!

As you will gather from the text body, we are trying to allow the user to select a discontiguous set of words in an {Index} and then have a macro process each of the selected words, one by one.
If I *HAVE* to, I’ll copy the text to a document then quick-as-flash process then delete that document.
But I’d rather just do this in RAM and not disturb the file system.

(signed) "At my wit’s end” of Toronto.

P.S.
http://support.microsoft.com/kb/288424
Limitations for working with discontiguous selections using the Word object model
• You cannot select a discontiguous range programmatically by using the Word object model. A discontiguous selection may only be created by the user in the Word UI.
• A Range object cannot contain multiple subranges. Therefore, if the current selection is discontiguous, Selection.Range will return the last subrange that the user selected.
• When the user selects multiple discontiguous ranges, the following methods and properties of the Selection object apply to all of the subranges in the user's selection:
Selection.Font.Bold
Selection.Font.Italic
Selection.Font.Size
Selection.ClearFormatting
Selection.Copy
Selection.Borders
Selection.Delete
Selection.Style
Selection.Find


NOTE: The Find method works, but you may experience problems keeping the discontiguous ranges selected.
• When the user selects multiple discontiguous ranges, the following members of the Selection object are applied only to the last subrange in the selection:
Selection.Words.Count
Selection.Characters.Count
Selection.Bookmarks.Count
Selection.Text
• Certain members of the Selection object generate one of the following run-time error messages when the selection contains multiple discontiguous ranges:

etc. etc. etc.

P.P.S.
http://windowssecrets.com/forums/showthread.php/10096-Discontiguous-Selections-in-VBA-Word-XP-%28Word-XP%29
But I can find no reference to “discontiguous” in the VBA forum.
You do not have the required permissions to view the files attached to this post.
There's nothing heavier than an empty water bottle

User avatar
HansV
Administrator
Posts: 78487
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Discontiguous selection in Word 2003

Post by HansV »

As noted in the MSKB article, support for discontiguous selections in the Word object model is incomplete. It is not intended to let you loop through the subranges, just to let you apply some actions to the discontiguous selection as a whole. If you keep that in mind, it can be useful in some situations. But it's clearly not the solution for what you had in mind.
Best wishes,
Hans

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 15621
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Re: Discontiguous selection in Word 2003

Post by ChrisGreaves »

HansV wrote:As noted in the MSKB article, support for discontiguous selections in the Word object model is incomplete.
Thanks for the confirmation, Hans.
It looks to me like another great idea that was pruned in the rush-to-deadline.
There's nothing heavier than an empty water bottle

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 15621
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Re: Discontiguous selection in Word 2003

Post by ChrisGreaves »

HansV wrote:But it's clearly not the solution for what you had in mind.
Still true, but it has dawned on me that since the user's requirements are two-fold the Selection.Copy then .Paste to a new document is a neat-enough solution.

1. The user wants me to build a text file e.g. Noise.txt from the discontiguous selection.
I have to create a file anyway, right?
I can achieve that by pasting into a new word document then saving that new document as a text file "Noise.txt"
Objective 1 achieved

2. The user wants me to eliminate all {XE} fields that contain any of the selected strings.
Well, it just so happens that I now have a text file, Noise.txt, which delivers to me a string of delimited strings, the words originally selected, so I can use Selection.Find to locate all occurrences of each of those sub-strings within fields in the document.
Objective 2 achieved

Mission accomplished!
There's nothing heavier than an empty water bottle