'For Each' paragraph in selection when selection discontinuous

R

rafraf

Best way to explain this problem is to demonstrate it. I have only
tried this with Word 2003 SP2.

In a block of three paragraphs, select the first and last paragraph (do
not select the middle paragraph) using ctrl-click. Now execute the
following macro:

Sub WorksWhenSelectionDiscontinuous()
Selection.Paragraphs.Indent
End Sub

As expected, the two paragraphs in your selection indent.

Now try this code with the same selection:

Sub DoesNotWorkWhenSelectionDiscontinuous()
Dim para As Paragraph
For Each para In Selection.Paragraphs
para.Indent
Next para
End Sub

This time, only the last paragraph in the selection indents. Note that
if you select all three paragraphs continuously (i.e. without the ctrl
key) then all paragraphs in the selection will, as expected, indent.

Can anyone explain or provide a workaround*?

*By workaround I mean a way of performing an action on each paragraph
in the selection such that I can do different things based on the
attributes of each paragraph in the (discontinuous) selection.

Cheers all.
 
G

Greg Maxey

rafraf,

I don't think there is much available in the way of working with
dicontinuous selections in Word. Here is a rough work around. Do something
funky to the selected text, then process the funky text and while processing
restore the original text.

Sub DoesNotWorkWhenSelectionDiscontinuous()
Dim para As Paragraph
Dim curColor As Long
curColor = Selection.Font.Color
Selection.Font.Color = wdColorBrightGreen
For Each para In ActiveDocument.Paragraphs
If para.Range.Font.Color = wdColorBrightGreen Then
para.Indent
para.Range.Font.Color = curColor
End If
Next para
End Sub
 
J

Jay Freedman

I can explain, but as far as I know there is no workaround.
Essentially, VBA has almost no support for discontinuous selections.
What little there is, is explained at
http://support.microsoft.com/?kbid=288424.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 
R

rafraf

Greg, thanks for that, it is a great idea. I'll play around and see if
there is a way of limiting the scope to the beginning of the first
selection and the end of the last selection when that selection is
discontinuous.
Jay, thanks for the link. It makes sense now. I suppose that in W2003
they must be doing something akin to
"Selection.ShrinkDiscontiguousSelection" because, unlike W2002, there
are no errors in 2003.

I am sorry for not finding the link myself. I did have a Google, just
didn't realize that discontiguous was a word :)

Cheers,

Raf.
 
H

Helmut Weber

Hi Rafraf,

you may use highlighting, like:
Highlight some text in different paragraphs.
Loop through all paragraphs that contain highlighted text.

In fact, IMHO, I don't see much difference between the selection
and otherwise defined pieces of a text.

Applying e.g. a special formatting to all paragraphs
in a discontiguous selection is not different from applying
it to all paragraphs with highlighted pieces of text.

Just a bit of coding. And as far as speed is concerned,
even with selection there is no "at once method",
it's always one after the other.

--
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