How do I detect Muliple selections and then how do I work with them?

P

Paul

Hello all,

This is my first post to any vba group. I am new to vba, but not new to
programming. I am using Word 2002 on a Windows machine.

Here is my question. If a user has selected a disjointed range of text, how
can I know this in VBA, and how can I work with or get references to the
selections?

TIA

- Paul
Schrum
 
S

Stan Scott

Paul,

In VBA, "selection.areas.count" returns the number of non-contiguous
sections selected. To get the reference of each area, use
"selection.areas(n).address".

Stan Scott
New York City
 
H

Helmut Weber

Hi Paul,
some time ago I asked the same question and found this answer:
form Cindy Meister, MVP:
Start quote
"... There's next to no programming interface for multiple, separate
selections. It's been a while since I tried anything with it, but I
think the only way to use it is as part of the WordBasic object model:
Wordbasic.selectSimilarFormatting
As I [Cindy] recall, I was told during the Word 2002 beta that the
team had wanted to expose this functionality to VBA, but had run into
problems (time, money), so wasn't able to do so. And apparently it
was "forgotten" as we came into the next version. Along with a number
of other things."
End quote.
 
P

Paul

Helmut Weber said:
Hi Paul,
some time ago I asked the same question and found this answer:
form Cindy Meister, MVP:
Start quote
"... There's next to no programming interface for multiple, separate
selections. It's been a while since I tried anything with it, but I
think the only way to use it is as part of the WordBasic object model:
Wordbasic.selectSimilarFormatting
As I [Cindy] recall, I was told during the Word 2002 beta that the
team had wanted to expose this functionality to VBA, but had run into
problems (time, money), so wasn't able to do so. And apparently it
was "forgotten" as we came into the next version. Along with a number
of other things."
End quote.

Helmut,

This is really disappointing.

What do you think about this. (I have yet to try it out, but I thought it
might be a way.) Shouldn't the disjoint selection be represented correctly
in Selection.Words? If so, then by comparing Selection.Words to
ThisDocument.Words word-for-word ought to indicate that it is a multiple
selection, shouldn't it. Well, that is what I am going to attempt, anyway.
Please let me know if any other ideas come forth.

- Paul
 
K

Klaus Linke

This is really disappointing.

Yep, that's the general consensus :-(

One thing you could do is to apply some kind of unique character
formatting, and then use Find to step through all ranges that have that
formatting, removing it as you go along.

Regards,
Klaus
 
H

Helmut Weber

Hi Paul,
yet, I think, there is no real need for a discontiguous
selection, though it would sometimes be convenient,
as often highlighting does the job as well,
or the use of formatting including text effects, as Klaus indicated.
 
P

Paul

Helmut Weber said:
Hi Paul,
yet, I think, there is no real need for a discontiguous
selection, though it would sometimes be convenient,
as often highlighting does the job as well,
or the use of formatting including text effects, as Klaus indicated.
---
For my application there is a need, although without it, I can devise a
workaround. I am attempting an application which would aid the user in
marking up the text grammatically. It is something like diagramming
sentences. In English (as of course you know becuase you are talking with
me in English), main verbs plus their helping verbs are sometimes split
apart. As a matter of fact, that last sentence has an example of such in
it: "are split" is the verb, but sometimes comes in between them. I want
the user to be able to designate "are split" as the verb and "sometimes" as
an adjective. Thus I need disjoint selections or I will have to kludge a
workaround which will be at the user level.

- Paul
 
H

Helmut Weber

Hi Paul,
I am hoping I am not obtrusive, but what I meant was,
that you don't have to use a split selection at all,
but could highlight verb and preposition e.g. in
yellow, and by making extensive use of colors,
could even distinguish between "off they went" and
"they went off". And there may even arise the question
of marking other kinds of discontinuous lexemes,
like "passers by", "well to do", "lookers on" etc.
Using colors you could distinguish between them all.
 
D

Dave Lett

Hi Paul,

What, in the end, do you want to do with your "disjoint selections"? If you
want to do the same thing to the selections, then you can simply use the
Selection object. In your example, as a user, I would highlight all the
verbs "are" and "split". Click the button to run your macro, which is

Selection.Font.Bold = True

or some other formatting.

HTH,
Dave
 
P

Paul

Helmut Weber said:
Hi Paul,
I am hoping I am not obtrusive,
Wow, this is the most polite group I have ever posted in. Thanks for your
attitude.
but what I meant was,
that you don't have to use a split selection at all,
but could highlight verb and preposition e.g. in
yellow, and by making extensive use of colors,
could even distinguish between "off they went" and
"they went off". And there may even arise the question
of marking other kinds of discontinuous lexemes,
like "passers by", "well to do", "lookers on" etc.
Using colors you could distinguish between them all.

Yes, I was thinking about those possibilities. However the focus of my
question is not how may I use a split selection. Rater, it is, if the user
chooses use a disjoint selection to indicate two words, how can I determine
this and how can I retrieve the range of each selection. As far as I can
tell, I can not detect disjoint selections in VBA. Therefore, even if I add
effects as nice as the ones you describe, the user must do his part without
the use of disjoint selections.

Help me out here. Am I still missing your point?

- Paul
 
P

Paul

Dave Lett said:
Hi Paul,

What, in the end, do you want to do with your "disjoint selections"? If you
want to do the same thing to the selections, then you can simply use the
Selection object. In your example, as a user, I would highlight all the
verbs "are" and "split". Click the button to run your macro, which is

Selection.Font.Bold = True

or some other formatting.

Dave,

Thanks for your response. There are quite a few things I would like to do
with any given selection. Right now, my basic functionality is to allow the
user to declare that a given selection is a certain part of speech.
Depending on what part of speech the user says the word is, the application
tabs the word over to a specified column.

Now I am already doing this (in an elementary way) with regular types of
selections. I was hoping to be able to allow the user to indicate a
noncontinguous grouping of words as a single unit of some type. I would
like to have phrases grouped with some intelligence as units, even when one
or more phrases are broken up by others.

In this example:

"For when Gentiles who do not have the Law do instinctively the things of
the Law, these, not having the Law, are a law to themselves."

The top-level phrase in that sentence is "For these are a law to
themselves." As I said before, I can provide the user with a way to build
the phrase a piece at a time, but if they could use the disjoint selection,
it would be much better for them.

By the way, from a little testing I did today, it looks like there is no way
I can get access to Word's disjoint selection information, so unless someone
knows a trick, I think it is likely not going to happen.

- Paul
 
H

Helmut Weber

Hi Paul,
use any not too complicated word-doc.
Highlight all 2-part-verbs, let's call them
disjoint verbs, like this:
[are] sometimes [split]
(Be sure, that number of highlighted words is even.)
Square brackets are used to indicate highlighting.
Collect all pairs of these verbs like:
Sub Test432()
Resetsearch
Dim Part1 As String
Dim Part2 As String
Dim oRng As Range
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = ""
.Highlight = True
While .Execute
Part1 = oRng.Text
.Execute
Part2 = oRng.Text
MsgBox "Found: " & Part1 & " " & Part2
' or debug.print or collect them in an
' array, count them, sort them, whatever
Wend
End With
Resetsearch
End Sub
---
or collect all words that are between the
two disjoint verbs
---
Sub Test332()
Resetsearch
Dim Part1 As String
Dim Part2 As String
Dim oRng As Range
Dim rTmp As Range
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = ""
.Highlight = True
While .Execute
Set rTmp = Selection.Range
Part1 = oRng.Text
rTmp.Start = oRng.End
.Execute
Part2 = oRng.Text
rTmp.End = oRng.Start
MsgBox "Found: " & rTmp.Text
' or debug.print or collect them in an
' array, count them, sort them, whatever
Wend
End With
Resetsearch
End Sub
---
Sub Resetsearch()
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute
End With
End Sub
---
HTH
Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word 2002, Windows 2000
 
P

Paul

Helmut Weber said:
Hi Paul,
use any not too complicated word-doc.
Highlight all 2-part-verbs, let's call them
disjoint verbs, like this:
[are] sometimes [split]
(Be sure, that number of highlighted words is even.)
Square brackets are used to indicate highlighting.
Collect all pairs of these verbs like:
Sub Test432()
Resetsearch
Dim Part1 As String
Dim Part2 As String
Dim oRng As Range
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = ""
.Highlight = True
While .Execute
Part1 = oRng.Text
.Execute
Part2 = oRng.Text
MsgBox "Found: " & Part1 & " " & Part2
' or debug.print or collect them in an
' array, count them, sort them, whatever
Wend
End With
Resetsearch
End Sub

Helmut,

This should do what I need. Thank you very much.

- Paul
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top