Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Centering a table in Word 1

Status
Not open for further replies.

JTBorton

Technical User
Jun 9, 2008
345
DE
I am trying to copy a table from an excel sheet and paste it into a word document. I have no problem copying the table, but I cannot get the table to center on the word document page. I am very unfamiliar with Word VBA. Can anyone help?

(Select and copy the table in excel)
(xlRng - an Excel range object)
With ActiveSheet
.
.(determines the range of the table)
.
Set xlRng = Range(.Cells(1, 1), .Cells((V - 1), 8))
xlRng.Copy
End With

(Paste it onto Word and hopefully center it)
(oWdoc - a word document object)
oWdoc.Sections(1).Range.Paste
With Selection.Tables(1).Rows <-- Crashes here
.LeftIndent = 0
.Alignment = wdAlignRowCenter
'or set whichever alignment you want
End With
 
With [!]oWdoc.Application.[/!]Selection.Tables(1).Rows

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
fumei:

Hey that's really interesing. I'm not sure exactly how you would use the style to center the table though. Here is how I have created and centered the table:

'Paste the copied Excel table in Word and center it
oWdoc.Sections(1).Range.Paste
With oWdoc.Application.Selection.Tables(1).Rows
.LeftIndent = 0
.Alignment = wdAlignRowCenter
End With

So would I instead say (Copying slightly from the microsoft help file...)

Dim styTable As Style

oWdoc.Sections(1).Range.Paste
Set styTable = ActiveDocument.Styles.Add(Name:="TableStyle 1", Type:=wdStyleTypeTable)

With styTable.Table
.LeftIndent = 0
.Alignment = wdAlignRowCenter
End With

...?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top