'For Each' paragraph in selection when selection discontinuous

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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.

.



Relevant Pages

  • Re: For Each paragraph in selection when selection discontinuous
    ... funky to the selected text, then process the funky text and while processing ... For Each para In ActiveDocument.Paragraphs ... In a block of three paragraphs, select the first and last paragraph ... the two paragraphs in your selection indent. ...
    (microsoft.public.word.vba.general)
  • Re: Help With Code
    ... am I correct if I would say you use Dim ... object (like the name of a paragraph style in your document)? ... > Greg Maxey/Word MVP ... >>> which may lead beginners to stick to the selection. ...
    (microsoft.public.word.vba.general)
  • Re: Moving Styles in a script
    ... Word already has a way to apply styles to the selection. ... your add-in. ... or the selected paragraph will be formatted other than as ... clikcs on a toolbar button which initiates the macro. ...
    (microsoft.public.word.formatting.longdocs)
  • Re. Module Comments
    ... comment between Sub or Function, ... to divide a written paragraph into lines of adequate length. ... 'cursor after the last character of the selection ... 'selection of the first character in the line ...
    (microsoft.public.excel.programming)
  • Re: Word 2000 Styles not in Word2007 Styles list
    ... If I'm correctly interpreting what you're asking, I think you have to set the priority of your preferred styles to be higher than that of the others in the Quick Style gallery. ... As for updating styles, if you make a change to a single paragraph in a given style and want to update the style to include that change, you can use Update Style to Match Selection. ...
    (microsoft.public.word.docmanagement)