View Full Version : Cell padding when copying from Excel
paddysheeran
02-19-2013, 06:11 AM
Hi All,
I have a procedure that works fine when copying data from Excel to word.
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
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:
With t
With .Cells(1)
.TopPadding = 3
.BottomPadding = 3
.LeftPadding = 2
.RightPadding = 2
.FitText = False
End With
End With
Does anyone have a quick fix for this? thanks.
fumei
02-19-2013, 01:34 PM
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.
paddysheeran
02-28-2013, 02:23 AM
the following work fine though with the t range:
With t
.Font.Size = "8"
.Tables(1).Rows(1).HeadingFormat = True
.Tables(1).Rows.AllowBreakAcrossPages = False
End With
and
With t.ParagraphFormat
.LeftIndent = 0
.SpaceBeforeAuto = False
.SpaceAfterAuto = False
.CharacterUnitLeftIndent = 0
End With
and
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
macropod
02-28-2013, 05:57 AM
For the padding, change:
With .Cells(1)
to:
With .Tables(1)
paddysheeran
02-28-2013, 06:35 AM
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
macropod
02-28-2013, 06:51 AM
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.
paddysheeran
02-28-2013, 08:09 AM
this only affects the overall table cell margins
When I record the code in word for amending the cell padding the code output is:
With Selection.Cells(1)
.TopPadding = 2
.BottomPadding = 2
.LeftPadding = 2
.RightPadding = 2
.FitText = False
End With
macropod
02-28-2013, 08:19 AM
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.
paddysheeran
02-28-2013, 08:24 AM
Sorry I require all cells within the imported table to have a padding
macropod
02-28-2013, 08:32 AM
Sorry I require all cells within the imported table to have a padding
And that is exactly what the change I suggested does.
paddysheeran
02-28-2013, 08:33 AM
Sorry you change doesn't work in my script - thanks for trying anyway
macropod
02-28-2013, 05:43 PM
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 ...
fumei
02-28-2013, 08:29 PM
Dim objTable As Table
Set objTable = t.Tables(1)
With objTable
.TopPadding = 2
.BottomPadding = 2
.LeftPadding = 2
.RightPadding = 2
End With
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.