Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / November 2005

Tip: Looking for answers? Try searching our database.

Iterating through multiple selected items

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Patrício - 28 Nov 2005 09:07 GMT
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.
Jay Freedman - 28 Nov 2005 15:52 GMT
> Hello,
>
[quoted text clipped - 6 lines]
>
> 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.

Signature

Regards,
Jay Freedman
Microsoft Word MVP          FAQ: http://word.mvps.org

Helmut Weber - 28 Nov 2005 16:23 GMT
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

Signature

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.