Solved

Shading Paragraphs using VBA

Posted on 2015-01-28
5
79 Views
Last Modified: 2015-01-30
Dear Experts:

for some specific reason I would like to shade every other 'product' (the list contains hundreds of these entries) as you can see in the screen shot using a macro

Help is very much appreciated, thank you very much in advance.

Regards, Andreas

apply grey shadingShading-every-other-product.docx
0
Comment
Question by:AndreasHermle
  • 3
  • 2
5 Comments
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 40576311
Hi Andreas.
If you use Shading, it extends between paragraphs. Highlighting would look more like your picture.
Here are macros for each:
Sub ShadeIt()
    Dim para As Paragraph
    Dim p As Integer
    
    For Each para In ActiveDocument.Range.Paragraphs
        If p Mod 4 > 1 Then
            para.Range.Shading.BackgroundPatternColor = wdColorGray15
        End If
        p = p + 1
    Next para
End Sub

Open in new window

Sub HighLightIt()
    Dim para As Paragraph
    Dim p As Integer
    
    For Each para In ActiveDocument.Range.Paragraphs
        If p Mod 4 > 1 Then
            para.Range.HighlightColorIndex = wdGray25
        End If
        p = p + 1
    Next para
End Sub

Open in new window

0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 40576315
I didn't see that you had posted a sample document, but the answer still stands.
0
 

Author Comment

by:AndreasHermle
ID: 40577083
Hi Graham,

sorry for the delay in giving a feedback. Thank you very much for your professional efforts, as always the macro runs nicely.

However there is one thing I would like to get changed:

As you can see in the screenshot, not the whole paragraph should be shaded but just the text and moreover ...
... I would like to run this macro on a selection.

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

Regards, Andreas
0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 500 total points
ID: 40578585
Hi Andreas, as I hinted in my answer, "Shading" in Word technology refers to a Range, so that it will colour the whole of the range and not just the individual characters. Highlighting, however,  does target individual characters, so, for working on the selection only,  I suggest a slight variation on my second suggestion
Sub HighLightIt()
    Dim para As Paragraph
    Dim p As Integer
    
    For Each para In Selection.Paragraphs
        If p Mod 4 > 1 Then
            para.Range.HighlightColorIndex = wdGray25
        End If
        p = p + 1
    Next para
End Sub

Open in new window

0
 

Author Closing Comment

by:AndreasHermle
ID: 40579954
Hi Graham, as always, thank you very much for your professional and swift support. Works as desired, and as always, great and detailed explanation.

Regards, Andreas
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Do you ever need to create a 20 page Word document for some testing purpose? Are you tired of copying & pasting old boring "lorem ipsum" text over and over again, increasing font size and line space in order to make the document 20+ pages long? Look…
Introduction This tutorial provides instructions on how to properly format your Word document using the inbuilt tools provided. The benefits of using these tools means your documents are more accessible and easily portable to other applications an…
This video shows the viewer how to set up and create Footnotes in their document. Click on the References tab: Select "Insert Footnote": Type in desired text:
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question