Solved

Shading Paragraphs using VBA

Posted on 2015-01-28
5
77 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

If you work with Word a lot, you probably use styles. If you use styles a lot, you've probably balled your fist more often than not when working with the ribbon. In Word 2007/2010, one of the things that I find missing when using styles is a quic…
This article describes how to use the Send to Mail Recipient command. The instructions apply generally to Office 2007 and later versions, but Microsoft® Word 2013 was used for the specific steps and figures.  What is Send to Mail Recipient? Send…
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.
This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …

895 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now