+ Reply to Thread
Results 1 to 2 of 2

Excel vba to word-multiple tables for each excel row

  1. #1
    Registered User
    Join Date
    07-14-2014
    Location
    india
    MS-Off Ver
    2007
    Posts
    1

    Excel vba to word-multiple tables for each excel row

    I have an excel sheet which I want to convert into a new word document with multiple tables.

    Basically i want each excel row as a new word table.

    The new table should have 2 columns.The excel row should now be converted into the second word column and the first column should have the corresponding header.

    Say for example, take the below data as in the excel sheet.


    ID NAME
    01 A
    02 B

    I want to convert the above data in each row as a separate word table. Here, ID and NAME are the headers..and they should be repeated as a column for each table as shown below

    ID 01
    NAME A



    ID 02
    NAME B


    and so on.... The word tables should be automatically generated depending on the no of rows in the excel sheet.

    This is the code I got up till now

    Sub CreateNewWordDoc()
    ' add a reference to the Word-library
    Dim wrdApp As Word.Application
    Dim wrdDoc As Word.Document
    Dim wrdTable As Word.Table
    Dim xText, xText2
    Dim i As Integer
    Set wrdApp = CreateObject("Word.Application")
    Set wrdDoc = wrdApp.Documents.Add ' create a new document
    Set wrdRange = wrdDoc.Range
    wrdApp.Visible = True

    For i = 1 To 10
    xText = Worksheets("Sheet1").Cells(i, 1).Value
    xText2 = Worksheets("Sheet1").Cells(i, 2).Value

    Set wrdRange = wrdDoc.Range
    With wrdRange
    .Collapse Direction:=wdCollapseEnd
    .InsertParagraphAfter
    .Collapse Direction:=wdCollapseEnd
    End With

    'Create table
    Set wrdTable = wrdDoc.Tables.Add(Range:=wrdRange, NumRows:=1, NumColumns:=2)

    With wrdTable
    With .Cell(i, 1).Range
    .InsertAfter xText
    End With
    xText = Worksheets("Sheet1").[a2].Value
    With .Cell(i, 2).Range
    .InsertAfter xText2
    End With
    End With
    Next i
    '------new code-----------

    'clean up
    Set wrdDoc = Nothing
    Set wrdApp = Nothing
    End Sub


    But this does not work for the repeated headers and conversion of the row into column. I can't figure out how to do it. Please help me with the same.

  2. #2
    Forum Expert Tinbendr's Avatar
    Join Date
    06-26-2012
    Location
    USA
    MS-Off Ver
    Office 2010
    Posts
    2,125

    Re: Excel vba to word-multiple tables for each excel row

    Welcome to the board!

    Please use code tags when posting code.

    Please Login or Register  to view this content.
    David
    (*) Reputation points appreciated.

  3. #3
    Registered User
    Join Date
    07-14-2014
    Location
    india
    MS-Off Ver
    2007
    Posts
    1

    Re: Excel vba to word-multiple tables for each excel row

    Thanks for your help!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Macro to Import Multiple Word Tables Into Excel Worksheet
    By bigkahuna2187 in forum Excel Programming / VBA / Macros
    Replies: 16
    Last Post: 06-10-2014, 03:18 PM
  2. [SOLVED] Need a macro to copy multiple word files with tables into excel workbook
    By neetharao in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-26-2012, 08:30 AM
  3. Import tables from multiple Word files into a sheet of Excel
    By magdy in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-27-2012, 03:53 AM
  4. Excel tables to Word
    By wildand in forum Excel General
    Replies: 2
    Last Post: 12-17-2010, 10:31 PM
  5. Word tables to Excel
    By Upparna in forum Excel - New Users/Basics
    Replies: 1
    Last Post: 04-15-2007, 02:33 PM

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1