Consulting

Results 1 to 13 of 13

Thread: Cell padding when copying from Excel

  1. #1

    Cell padding when copying from Excel

    Hi All,

    I have a procedure that works fine when copying data from Excel to word.

    [vba]Public wdApp As New Word.Application
    Public wdDoc As Word.Document
    Public t As Word.Range
    Public wsSource As Worksheet
    Set t = wdDoc.Bookmarks("Example 1").Range

    Set wsSource = ActiveWorkbook.Sheets("Example 1")
    wsSource.Activate

    Range("A1").CurrentRegion.Copy
    t.Paste

    [/vba]

    After the table has been entered into Word I want to adjust the cell padding. I've tried the following but this has no affect on the final table:

    [vba]With t
    With .Cells(1)
    .TopPadding = 3
    .BottomPadding = 3
    .LeftPadding = 2
    .RightPadding = 2
    .FitText = False
    End With
    End With[/vba]


    Does anyone have a quick fix for this? thanks.

  2. #2
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    With t

    t is declared as a range, not a table. True, the Excel table (region) is inserted into Word, but what is there is not a Word table.

  3. #3
    the following work fine though with the t range:

    [vba]With t
    .Font.Size = "8"

    .Tables(1).Rows(1).HeadingFormat = True
    .Tables(1).Rows.AllowBreakAcrossPages = False
    End With[/vba]

    and

    [vba] With t.ParagraphFormat
    .LeftIndent = 0
    .SpaceBeforeAuto = False
    .SpaceAfterAuto = False
    .CharacterUnitLeftIndent = 0

    End With[/vba]

    and

    [vba]With t 'Table Width Examples
    .Tables(1).Columns(1).SetWidth ColumnWidth:=38, RulerStyle:= _
    wdAdjustNone
    .Tables(1).Columns(2).SetWidth ColumnWidth:=40.65, RulerStyle:= _
    wdAdjustNone
    .Tables(1).Columns(3).SetWidth ColumnWidth:=41.55, RulerStyle:= _
    wdAdjustNone
    .Tables(1).Columns(4).SetWidth ColumnWidth:=41.55, RulerStyle:= _
    wdAdjustNone
    .Tables(1).Columns(5).SetWidth ColumnWidth:=47.75, RulerStyle:= _
    wdAdjustNone
    .Tables(1).Columns(6).SetWidth ColumnWidth:=88.95, RulerStyle:= _
    wdAdjustNone
    End With[/vba]

  4. #4
    VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    For the padding, change:
    With .Cells(1)
    to:
    With .Tables(1)
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  5. #5
    I've tried that but there is no cell padding in the final tables

    When I select the table in the final word document > Table properties > Cell > Options. The margins are all blank

  6. #6
    VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Aside from having to delete the '.FitText = False' line, using 'With .Tables(1)' works fine for me. This change will work anywhere that your:
    With t 'Table Width Examples
    .Tables(1).Columns(1).SetWidth
    code works.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  7. #7
    this only affects the overall table cell margins

    When I record the code in word for amending the cell padding the code output is:

    [VBA]With Selection.Cells(1)
    .TopPadding = 2
    .BottomPadding = 2
    .LeftPadding = 2
    .RightPadding = 2
    .FitText = False
    End With[/VBA]

  8. #8
    VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Selection.Cells(1) applies to one cell only - the first selected one. If you don't want all cells formatted that way, you need to say what it is you do want. Merely saying "I've tried the following but this has no affect on the final table" is both wrong (With t ... With .Cells(1) affects the first cell) and fails to indicate what you want to achieve.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  9. #9
    Sorry I require all cells within the imported table to have a padding

  10. #10
    VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Quote Originally Posted by paddysheeran
    Sorry I require all cells within the imported table to have a padding
    And that is exactly what the change I suggested does.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  11. #11
    Sorry you change doesn't work in my script - thanks for trying anyway

  12. #12
    VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    It does when I run the snippet you posted. Of course, without seeing the snippet in its context, there's no way of knowing what it applies to ...
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  13. #13
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    [VBA]
    Dim objTable As Table
    Set objTable = t.Tables(1)
    With objTable
    .TopPadding = 2
    .BottomPadding = 2
    .LeftPadding = 2
    .RightPadding = 2
    End With [/VBA]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •