Customize Cell Padding in Word Tables

A

andreas

Dear Experts:

I got a word 2003 document in which all the tables do not have any
left or right cell padding.
I tried to customize the left and right cell padding of a selected
Word table with the following code.
But nothing happens. The left and right cell padding remain at 0 cm.
Only the first part of the code seems to work (but this has got no
effect on the left and right cell padding), the second part starting
with 'With Selection.Cells(1) ... obviously has got no effect
whatsoever. Is this a bug?

Help is much appreciated. Thank you very much in advance. Regards,
Andreas


Sub CustomizeCellPadding()

(PART 1 of the code)
Selection.Tables(1).Select
With Selection.Tables(1)
.TopPadding = CentimetersToPoints(0)
.BottomPadding = CentimetersToPoints(0)
.LeftPadding = CentimetersToPoints(0.2)
.RightPadding = CentimetersToPoints(0.2)
End With

(PART 2 of the code)
Selection.Tables(1).Select
With Selection.Cells(1)
.TopPadding = CentimetersToPoints(0)
.BottomPadding = CentimetersToPoints(0)
.LeftPadding = CentimetersToPoints(0.2)
.RightPadding = CentimetersToPoints(0.2)
.WordWrap = True
.FitText = False
End With

End Sub
 
P

Pesach Shelnitz

Hi andreas,

I tested your macro on a 4 x 4 table and found that it works and does what
the code specifies. We may be interpreting the results differently because we
may have different understandings of the operations that the macro performs.

In PART 1, the padding properties are the padding properties of the table as
a whole. These properties hold the default values for all the cells and
specify the padding for all the cells in the table for which you don't
specify different values. Since the default padding values that Word sets
when you create a table are very close (or even identical) to the values that
your macro sets, you can more clearly see that PART 1 is working by changing
the values from 0.2 to 0.3.

Also, the way you use the Selection object requires that the cursor be
somewhere in the table. If the cursor is outside of the table, you will get
an error because there is no table in the selection or at the insertion
point. For this reason, in my opinion, it would be better to use the
ActiveDocument object instead of the Selection object. Thus, to demonstrate
that PART 1 is working fine, I would change it to the following:

' PART 1
With ActiveDocument.Tables(1)
.TopPadding = CentimetersToPoints(0)
.BottomPadding = CentimetersToPoints(0)
.LeftPadding = CentimetersToPoints(0.3)
.RightPadding = CentimetersToPoints(0.3)
End With

PART 2 appears not to do anything because it sets properties of the first
cell in the table to their default values. In particular, it sets the padding
properties of the first cell in the table to the values that were set in PART
1. What exactly are you trying to do in PART 2?

Hope this helps,
Pesach Shelnitz
 
A

andreas

Hi andreas,

I tested your macro on a 4 x 4 table and found that it works and does what
the code specifies. We may be interpreting the results differently because we
may have different understandings of the operations that the macro performs.

In PART 1, the padding properties are the padding properties of the tableas
a whole. These properties hold the default values for all the cells and
specify the padding for all the cells in the table for which you don't
specify different values. Since the default padding values that Word sets
when you create a table are very close (or even identical) to the values that
your macro sets, you can more clearly see that PART 1 is working by changing
the values from 0.2 to 0.3.

Also, the way you use the Selection object requires that the cursor be
somewhere in the table. If the cursor is outside of the table, you will get
an error because there is no table in the selection or at the insertion
point. For this reason, in my opinion, it would be better to use the
ActiveDocument object instead of the Selection object. Thus, to demonstrate
that PART 1 is working fine, I would change it to the following:

    ' PART 1
    With ActiveDocument.Tables(1)
        .TopPadding = CentimetersToPoints(0)
        .BottomPadding = CentimetersToPoints(0)
        .LeftPadding = CentimetersToPoints(0.3)
        .RightPadding = CentimetersToPoints(0.3)
     End With

PART 2 appears not to do anything because it sets properties of the first
cell in the table to their default values. In particular, it sets the padding
properties of the first cell in the table to the values that were set in PART
1. What exactly are you trying to do in PART 2?

Hope this helps,
Pesach Shelnitz











- Show quoted text -

Hi Pesach,

thank you very much for your quick help. In the meantime I solved the
cell padding problem myself. The code is working just fine with me.
Thank you. Regards, Andreas

Sub TblCellPaddOneClick()
Dim myCell As cell
Dim myRow As row
Dim myTable As table

If Not Selection.Information(wdWithInTable) Then
MsgBox "Sie können das Makro nur aufrufen, wenn " & _
"sich der Cursor in einer Tabelle befindet!", _
vbOKOnly + vbCritical, "Standard-Format für Tabelle"
Exit Sub

End If


Set myTable = Selection.Tables(1)


With myTable
.TopPadding = CentimetersToPoints(0)
.BottomPadding = CentimetersToPoints(0)
.LeftPadding = CentimetersToPoints(0.19)
.RightPadding = CentimetersToPoints(0.19)
.Spacing = 0
.AllowPageBreaks = False
.AllowAutoFit = True
End With


For Each myRow In myTable.rows
For Each myCell In myRow.Cells
myCell.TopPadding = CentimetersToPoints(0)
myCell.BottomPadding = CentimetersToPoints(0)
myCell.LeftPadding = CentimetersToPoints(0.19)
myCell.RightPadding = CentimetersToPoints(0.19)
Next
Next


End Sub
 

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