PDA

View Full Version : Insert multiple tables in Word via Excel macro



Quinn
03-15-2006, 02:33 PM
I have an Excel table with N rows and would like to automatically create a Word document
that has N-1 tables on N-1 pages. The table would be comprised of two columns, the first column is the first row of the Excel worksheet and the second column of the table would be the next row in the Excel sheet. So far I have transposed using Excel, and then tried to loop counting the number of rows. But I am having trouble adding the tables to a single word document, my code instead creates n-1 word documents. I am weak in working with Word objects I guess?
any help would be appreciated...absolute newby

ARRAY1 = Array(Range("A1:V1").Value)
Range("a15:a36").Value = Application.WorksheetFunction.Transpose(ARRAY1)

For X = 1 To 3

Array2 = Array(Range("A1:V1").Offset(X, 0).Value)
Range("B15:B36").Value = Application.WorksheetFunction.Transpose(Array2)


Range(("a15:b36")).Select
then paste the range into a table...

mdmackillop
03-15-2006, 05:41 PM
Hi Quinn
Welcome to VBAX


Option Explicit
Sub Tables()

'This code requires a referece to the Word object model
Dim Appword As New Word.Application
Dim wdDoc As Word.Document
Dim Array1, Array2, X As Long

Set Appword = CreateObject("Word.Application")
Appword.Documents.Add

Array1 = Array(Range("A1:V1").Value)
Range("a15:a36").Value = Application.WorksheetFunction.Transpose(Array1)

For X = 1 To 3
Array2 = Array(Range("A1:V1").Offset(X, 0).Value)
Range("B15:B36").Value = Application.WorksheetFunction.Transpose(Array2)

Range("A15:B36").Copy

Appword.Selection.Paste
Appword.Selection.InsertBreak
Next
Appword.Visible = True

End Sub