Table Cell Formatting via VBA

A

Alex

Hi Everyone

I'm trying to create a piece of VBA code that will apply
preset cell shading and border formatting to either a
single cell or a group of selected cells, see below code:

Sub BlueCell()

With Selection.Shading
.Texture = wdTextureNone
.ForegroundPatternColor = wdColorAutomatic
.BackgroundPatternColor = RGB(200, 225, 250)
End With

Options.DefaultBorderLineStyle = wdLineStyleSingle
Options.DefaultBorderColor = RGB(0, 125, 200)
Options.DefaultBorderLineWidth = wdLineWidth050pt

With Selection.Borders
.OutsideLineStyle = Options.DefaultBorderLineStyle
.OutsideLineWidth = Options.DefaultBorderLineWidth
.OutsideColor = Options.DefaultBorderColor
End With

For Each Border In Selection.Borders
If Border.Inside = True Then
Border.LineStyle = Options.DefaultBorderLineStyle
Border.LineWidth = Options.DefaultBorderLineWidth
Border.Color = Options.DefaultBorderColor
End If
Next Border
End Sub


What I'm finding is that the codes works fine if I have
more than one table cell selected however, if I have a
single cell selected the code falls over within the IF...
Then... Else... statement at the:

Border.LineWidth = Options.DefaultBorderLineWidth


Can anyone help?

Big thank you in advance

Al
 
K

Klaus Linke

Hi Alex,

Looks like a bug to me:
With a single cell selected, there should be no inside borders, but
Selection.Borders.Count is still 8 (top, left, bottom, right, inside
horízontal and vertical, 2 diagonal).

As a work-around, you could use
If Border.Inside = True And Selection.Cells.Count > 1 Then

Greetings,
Klaus
 
A

Alex

Hi Klaus

That worked a treat.


I assuming that you are the same Klaus Linke as mentioned
on 'ListNumbering Street, Revisited'.

I'd just like to say on behalf of all us long suffering
Word users, excellent work.

Many thanks

Alex
 

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