Customize Cell Padding in Word Tables

Posted on 2009-05-02
Last Modified: 2012-05-06
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)


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)  


    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

Open in new window

Question by:AndreasHermle
    LVL 2

    Expert Comment

    I don't understand why you can't just highlight the whole table and then go to table properties > cell tab > Options button > and set margins for the cells right and left there.  

    If you just need the margin on the right and left side of the whole table just set a right margin for the cells in the right column and a left margin for the cells in the left column, ect.

    Author Comment

    Hi stiffstock,  
    ok,  I am  fully aware how to set the margins by means of the table properties menu.But I would like to alter these settings by just pressing a macro button for this purpose on a customized tool bar.  

    Regards, Andreas

    Accepted Solution

    Dear Stiffstock,

    found out myself how  to  solve my problem. Thank you anyway. Regards, Andreas
    Sub TblCellPadding()
    Dim myCell As cell
    Dim myRow As row
    Dim myTable As table
    If Not Selection.Information(wdWithInTable) Then
        MsgBox "The code will only be effected, if" & _
        "the cursor is inside the table!", _
         vbOKOnly + vbCritical, "Customize Cell Padding of selected tbl"
         Exit Sub
    End If
    Set myTable = Selection.Tables(1)
    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)
    End Sub

    Open in new window


    Featured Post

    Courses: Start Training Online With Pros, Today

    Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

    Join & Write a Comment

    Like many others, we try and discourage users from printing documents unnecessarily and instead send or share them electronically. However, this doesn't always work and documents are still printed. With this simple solution, if the user tries to …
    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…
    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:
    This video walks the viewer through the process of creating Hyperlinks for the web and other documents. Select the "Insert" tab: Click "Hyperlink":  Type "http://" followed by a web address to reference a website or navigate to a document to ref…

    754 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

    19 Experts available now in Live!

    Get 1:1 Help Now