Iterating through multiple selected items

P

Patrício

Hello,

I have multiple objects selected in a document, when i try to work with
the Selection (Selection.Text) object it is placed in the last selected
item in the document. What can i do to place it in the first position
and than iterate through the multiple elements?

Thanks in advance.

Paulo.
 
J

Jay Freedman

Patrício said:
Hello,

I have multiple objects selected in a document, when i try to work
with the Selection (Selection.Text) object it is placed in the last
selected item in the document. What can i do to place it in the first
position and than iterate through the multiple elements?

Thanks in advance.

Paulo.

Hi Paulo,

Microsoft never implemented the parts of VBA needed to work with multiple
selections. The limited number of things you can do are documented here:
http://support.microsoft.com/?kbid=288424

Here's a workaround that may help:

- Declare a new character style in the document. Make its formatting just
Default Paragraph Text (in other words, it doesn't apply any formatting that
isn't already there).

- Apply that character style to the Selection. All the subranges of the
multiple selection will receive the style.

- Declare a Range object, and use its .Find property to iterate through all
the ranges having the character style. Within each found subrange, do
whatever processing you need.
 
H

Helmut Weber

Hi Paulo,
I have multiple objects selected in a document, when i try to work with
the Selection (Selection.Text) object it is placed in the last selected
item in the document. What can i do to place it in the first position
and than iterate through the multiple elements?

You could do a lot of difficult and tedious programming.

As far as I know know, coding for the discontiguous selection
by MS was never finished.

However, if you really want to have it, and if all
of the text in the selection is formatted the same way:

Remember the formatting of the selection.
Check for a formatting, not used otherwise in the doc.
Apply this formatting to the selection.
All parts of the discontiguous selection
are now formatted in a way, unique to the doc.
Search for this formatting, using the range-object.
Insert the text you like, search again.
Having finished, restore the original formatting.
Plus lots of complications.

Yu may even create on array of ranges,
defined by the formatting.

Nothing but a workaround, of course,
but some people enjoy things like that.

Here is an example for counting, how many parts
this kind of selection has:

Function NumberOfSelections() As Long
Dim rDcm As Range
Set rDcm = ActiveDocument.Range
Selection.Font.Color = wdColorRed
With rDcm.Find
.Font.Color = wdColorRed
' assumed that there is no red text
.Format = True
While .Execute
NumberOfSelections = NumberOfSelections + 1
Wend
End With
Set rDcm = ActiveDocument.Range
With rDcm.Find
.Font.Color = wdColorRed
.Format = True
While .Execute
rDcm.Font.Color = wdColorAutomatic ' assumed
rDcm.Collapse direction:=wdCollapseEnd
Wend
End With
End Function

Sub test9001()
MsgBox NumberOfSelections
End Sub

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 

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