PDA

View Full Version : Formatting table in Word using Excel VBA



turkey
08-14-2007, 09:07 AM
I'm using the following to copy a table in Excel and paste it into Word.

Set wordApp = CreateObject("Word.Application")
wordApp.Visible = True
Set wordDocument = wordApp.Documents.Add
wordDocument.Paragraphs(wordDocument.Paragraphs.Count).Range.InsertParagrap hAfter
wordDocument.Paragraphs(wordDocument.Paragraphs.Count).Range.Paste

But when the table is placed in word, it does not fit on the page and half of it is cut off. How can I make the table autofit in word? I also want to make header rows repeat.

geekgirlau
08-20-2007, 09:31 PM
Dim WordApp As Word.Application
Dim WordDocument As Word.Document


Set WordApp = CreateObject("Word.Application")
WordApp.Visible = True

Set WordDocument = WordApp.Documents.Add

With WordDocument
.Range.InsertParagraph
.Range.Paste

With .Tables(1)
.Rows(1).HeadingFormat = True
.AutoFitBehavior wdAutoFitWindow
End With
End With

WordApp.Activate