Re: 'For Each' paragraph in selection when selection discontinuous

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



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


--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.


rafraf wrote:
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: 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)
  • Re: Delete single newline between tables
    ... The tool requires a selection prior to performing certain operations. ... Dim myPara as Paragraph ... For Each myPara in ActiveDocument.Paragraphs ... only drawback is that it can be a bit slow evaluating every paragraph in the ...
    (microsoft.public.word.vba.general)