Consulting

Results 1 to 2 of 2

Thread: Insert multiple tables in Word via Excel macro

  1. #1
    VBAX Regular
    Joined
    Mar 2006
    Location
    Indianapolis
    Posts
    14
    Location

    Thumbs up Insert multiple tables in Word via Excel macro

    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...

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Hi Quinn
    Welcome to VBAX

    [vba]
    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

    [/vba]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Posting Permissions

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