Macro to apply alternate grey/white shading to selected paragraphs

A

andreas

Dear Experts:

I would like to do paragraph shading using grey shading alternately.

Example:

this is a sample text
This is another sample text
This is yet another sample text
This may be the last paragraph acquiring grey shading
This is the penultimate paragraph
This is the last paragraph

With these 6 paragraphs selected, I would like to run a macro which
applies grey shading alternately to the selected paragraphs, i.e. grey/
white (no shading) alternately.

Help is much appreciated. Thank you very much in advance.

Regards, Andreas
 
G

Graham Mayor

Use

Dim oRng As Range
For i = 2 To ActiveDocument.Paragraphs.Count Step 2
Set oRng = ActiveDocument.Paragraphs(i).Range
oRng.Shading.BackgroundPatternColor = wdColorGray10
Next i

If you want the first row grey instead of the second, change to i = 1 etc.


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
A

andreas

Use

Dim oRng As Range
For i = 2 To ActiveDocument.Paragraphs.Count Step 2
    Set oRng = ActiveDocument.Paragraphs(i).Range
    oRng.Shading.BackgroundPatternColor = wdColorGray10
Next i

If you want the first row grey instead of the second, change to i = 1 etc.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor -  Word MVP

My web sitewww.gmayor.com
Word MVP web sitehttp://word.mvps.org
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>










- Zitierten Text anzeigen -

Hi Graham,

thank you very much for your swift response. It works very well,
although I would like the macro to react slightly differently.

The whole paragraphs get shaded, i.e. all the way from the left page
margin to the right page margin.

But I would like the shading to span only from the first to the last
character of the text. And this text may be much shorter than the
printable area of the page (page width minus right page margin minus
left page margin.

I hope I could make myself clear. Thank you very much in advance for
your help.

Regards, Andreas
 
F

Fumei2 via OfficeKB.com

Try:

Sub ShadeEm()
Dim oPara As Paragraph
Dim j As Long
j = 1
For Each oPara In Selection.Paragraphs
If j Mod 2 = 0 Then
oPara.Range.HighlightColorIndex = wdGray25
End If
j = j + 1
Next
End Sub

As you only want the paragraphs selected, I used Selection.Paragraphs. The
above shades (25%) every SECOND paragraph - using Mod 2 iterating through the
paragraph count of the Selection. If you want it the other way (the first
paragraph gets shaded and then alternates after), change to:

If j Mod 2 <> 0 Then


Gerry
[quoted text clipped - 36 lines]
- Zitierten Text anzeigen -

Hi Graham,

thank you very much for your swift response. It works very well,
although I would like the macro to react slightly differently.

The whole paragraphs get shaded, i.e. all the way from the left page
margin to the right page margin.

But I would like the shading to span only from the first to the last
character of the text. And this text may be much shorter than the
printable area of the page (page width minus right page margin minus
left page margin.

I hope I could make myself clear. Thank you very much in advance for
your help.

Regards, Andreas
 
A

andreas

Try:

Sub ShadeEm()
Dim oPara As Paragraph
Dim j As Long
j = 1
For Each oPara In Selection.Paragraphs
   If j Mod 2 = 0 Then
      oPara.Range.HighlightColorIndex = wdGray25
   End If
   j = j + 1
Next
End Sub

As you only want the paragraphs selected, I used Selection.Paragraphs.  The
above shades (25%) every SECOND paragraph - using Mod 2 iterating throughthe
paragraph count of the Selection.  If you want it the other way (the first
paragraph gets shaded and then alternates after), change to:

   If j Mod 2 <> 0 Then

Gerry




[quoted text clipped - 36 lines]
- Zitierten Text anzeigen -
Hi Graham,
thank you very much for your swift response. It works very well,
although I would like the macro to react slightly differently.
The whole paragraphs get shaded, i.e. all the way from the left page
margin to the right page margin.
But I would like the shading to span only from the first to the last
character of the text. And this text may be much shorter than the
printable area of the page (page width minus right page margin minus
left page margin.
I hope I could make myself clear. Thank you very much in advance for
your help.
Regards, Andreas

Hi Fumei,

great, that did the trick. Thank you very much for your terrific
help.

Regards, Andreas
 

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