Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Shading Paragraphs using VBA

Posted on 2015-01-28
5
Medium Priority
?
87 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

Introduction Authors who set out to write any sort of lengthy piece for online submission—be it a long question or comment on a technical form, an article, or a substantial blog entry—often find it useful to work up a draft in an editor other t…
Nice table. Huge mess. Maybe this was something you created way back before you figured out tabs or a document you received from someone else. Either way, using the spacebar to separate the columns resulted in a mess. Trying to convert text to t…
This video walks the viewer through the process of creating a watermark for their document, customizing it, and saving it for viewing/printing needs.
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 …

604 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