Transfer Information from Excel Table to Pre-formatted Word Table

  • I would like to read information from a table in Excel and input that information into a pre-formatted table in Word. I would like the user to be able to select an Excel file, tab and range of cells to be transferred to the Word document.


    So far I can have the user select an Excel file using the FileDialogFilePicker. I am having trouble selecting a range of cells from this file because the usual Excel Methods are not available in Word VBA.


    Any ideas, suggestions or directions? Thanks!

  • Re: Transfer Information from Excel Table to Pre-formatted Word Table


    Please post your existing code (Remembering the rules regarding code tags).


    What you want to do is relatively easy, but your code is needed so the solution can be given in the context of what you already have.

  • Re: Transfer Information from Excel Table to Pre-formatted Word Table


    Here is my existing code. I was hesitant to include it because it is pretty sloppy and doesn't work (hence the large commented section). The section above "Test Code" works fine but the section below is what I'm playing with. I borrowed the code from online and have been modifying it to no avail. If anyone has a more simple idea I'd be willing to completely botch what I have. :smile: Thanks so much!!


  • Re: Transfer Information from Excel Table to Pre-formatted Word Table


  • Re: Transfer Information from Excel Table to Pre-formatted Word Table


    Wow, you coding is immensely helpful. Thank you very much!


    There are a few more things I'd like to be able to do:


    1. I want to add columns to the Word table if there are not enough and delete columns if there are too few.
    I added a few lines of coding using "Select Case" to achieve this (see below). My problem now is that the header row has merged cells, so when I add columns, the first cell in each added column is a long merged cell. This throws off the formatting of the entire table. Any ideas as to how to avoid this issue? If I could add columns excluding the header row, I think that would work.

    2. Delete the extra row added as a result of looping through the Word table cells.

    I like how you were able to call the table dynmically by selecting the table and then referring to its count (ActiveDocument.Tables(ActiveDocument.Range(0, Selection.Tables(1).Range.End).Tables.Count)). Is there a way to modify this so as to select just the bottom row? If so, I think I could just delete the row from there using "selection.delete".


  • Re: Transfer Information from Excel Table to Pre-formatted Word Table


    Just to be clear - you mentioned that the table is pre-formatted.


    I took that to mean that you have various font and/or colour attributes set in the cells that you wanted to preserve as/if rows are added. What happens with the additional columns?


    And where are the headings coming from? Excel, or are they already in the Word document?

  • Re: Transfer Information from Excel Table to Pre-formatted Word Table


    Yes, the table is pre-formatted with font, size, bold attributes. There are 2 headers;
    (1) Title header containing the title of the table (merged cells), and
    (2) header containing description of the columns.
    The first header should never change. The second header will be filled in using the values from the Excel table. Formatting for additional rows should preserve formatting in the last row of the table. Formatting for additional columns should preserve formatting in the last column (minus the first header with merged cells).


    A sample of the table can be seen below. Does that help clarify? Sorry to be vague!
    [TABLE="width: 653"]

    [tr]


    [td]


    [/td]


    [TD="colspan: 11"]Years of service
    [/TD]

    [/tr]


    [tr]


    [td]

    Age

    [/td]


    [td]

    Under 1

    [/td]


    [td]

    1-4

    [/td]


    [td]

    5-9

    [/td]


    [td]

    10-14

    [/td]


    [td]

    15-19

    [/td]


    [td]

    20-24

    [/td]


    [td]

    25-29

    [/td]


    [td]

    30-34

    [/td]


    [td]

    35-39

    [/td]


    [td]

    40+

    [/td]


    [td]

    Total

    [/td]


    [/tr]


    [tr]


    [td]

    Under 25

    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [/tr]


    [tr]


    [td]

    25 - 29

    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [/tr]


    [tr]


    [td]

    30 - 34

    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [/tr]


    [tr]


    [td]

    35 - 39

    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [/tr]


    [tr]


    [td]

    40 - 44

    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [/tr]


    [tr]


    [td]

    45 - 49

    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [/tr]


    [tr]


    [td]

    50 - 54

    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [/tr]


    [tr]


    [td]

    55 - 59

    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [/tr]


    [tr]


    [td]

    60 - 64

    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [/tr]


    [tr]


    [td]

    65 - 69

    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [/tr]


    [tr]


    [td]

    70+

    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [/tr]


    [tr]


    [td]

    TOTAL

    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [td]


    [/td]


    [/tr]


    [/TABLE]

  • Re: Transfer Information from Excel Table to Pre-formatted Word Table


    Makes sense now...


    It's getting a little late so might be falling asleep soon, but will have a closer look at this tomorrow - it's a lot more interesting than some of the dross that has been posted on here lately :)


    Two more questions first, though... is there only one table in the document, or potentially several? And do the Row headers (50 - 54 etc...) come from excel and the font in that column should be bold as well?

  • Re: Transfer Information from Excel Table to Pre-formatted Word Table


    Haha, get some sleep! To answer your questions:

    (1) There are several tables in the document. Since I'm so new to VBA I was just trying to get coding to work for one table. However, if you have ideas for making it work for several that would be wonderful!


    (2) The row headers names come from Excel and should be bold. However, the text inside the table (under the column header and to the right of the row header) is not bold.

  • Re: Transfer Information from Excel Table to Pre-formatted Word Table


    This might be a little closer to what you want...


    With hindsight, you don't actually need to merge the cells in row 1 either. You can just have the exact number of columns needed (I assumed 2), the columns will be sized to fit in code.


    The table is split because the number of columns may not match in all rows... and Word can have problems when adding columns in that situation.


    Sample of the table I used to develop and test attached. (Just remembered, does not contain the code to delete existing data in table)

  • Re: Transfer Information from Excel Table to Pre-formatted Word Table


    Very clever execution there. Thanks so much! I did make one modification. When changing the column width of row 1 (at the end of the code) I set the widths equal to the cell widths in row 2. Otherwise the length is set too long.


    Code
    '// Set width of Column 1 in Original table to current width of Col 1 in Data table
        wrdTable.Cell(1, 1).Width = wrdDataTable.Cell(2, 1).Width
         
         '// Set width of Column 2 in Original table to match the remaining width of the data table
        wrdTable.Cell(1, 2).Width = wrdDataTable.Cell(2, 2).Width * (wrdDataTable.Columns.Count - 1)


    On another note, if you're not completely sick of me I have another task I'm working on with this project. I'd like to be able to select the table to be modified instead of having the cursor in a table. In its present state, my table is #3 in the document. So I can set the cursor to be in cell(1,1) of table 3. However, if a table gets added before the one I want modified, the table number will change.


    Do you know if there is a way to dynamically select a table? I thought there might be a way to name the table within Word but I can't find anything of that nature. Right now, instead of putting the cursor in a table, I am using this code:


    Code
    Set ActDistTable = wrdDoc.Tables(3)
    ActDistTable.Cell(1,1).Select
  • Re: Transfer Information from Excel Table to Pre-formatted Word Table


    Working on it. The suggested solution is not persistent. Set an ID string, save the document, IDs are not saved.


    Alternative is to use bookmarks - code to follow.
    ________________________________________________________________________________________________________
    This thread has been moved from the general Excel forum to the Word forum as the primary Office component referenced is Word.

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!