PDA

View Full Version : Automatically inserting a table row for each merge record



dpeisenbeisz
09-21-2017, 11:45 AM
I'm sure this has been asked and answered, but I can't seem to find it, and searching for something as generic as "Word Tables" brings up so many posts I could spend all day looking through them. Please point me to the thread or web page that will help me overcome this simple problem.

I know enough about office VBA and other programming languages to be dangerous, but I'm not completely ignorant of this type of programming either. What I am trying to do is pull data from an Excel spreadsheet into a Word (2016) table. The spreadsheet could have anywhere from 3 to 200 rows of data. Each row has the same number of columns, but not all of them will be imported.

I want to be able to enter my data using Excel because it allows me to sort and perform calculations within the data that Word cannot do easily. Once I have the data arranged and calculated, I need to import the important cells of each row into each row of a word table to generate a report.

I've tried to do this using XML, a comma-delimited import, and a mail merge. I am not opposed to any of these solutions. XML works better when importing INTO Excel, not so great the other way around but I still have the same problem. A mail merge works a little better, but I still need to determine the correct number of rows, then insert the rows, then manually populate each row with merge fields, which kind of makes it non-automated.

So, I think a good solution would be some bit of code that will take the second row of my table (the first row is a header) and repeat it for each record (row) in my Excel file. Again, the issue is adding the correct number of rows to a pre-formatted table.

Any thoughts on how to setup a doc template for doing this? Is XML or a merge a better way to go, or something else entirely? I'm not looking for a solution, just a point in the right direction. Thanks.

macropod
09-21-2017, 05:45 PM
Word can do quite complex calculations. To see how to do a wide range of calculations in Word, check out my Microsoft Word Field Maths Tutorial, at:
http://windowssecrets.com/forums/showthread.php/154369-Microsoft-Word-Field-Maths-Tutorial
or:
http://www.gmayor.com/downloads.htm#Third_party

Nevertheless, if you're wedded to using Excel, you could name the range there, then copy & paste that into your Word document using Paste Special with the 'paste link' option and the desired paste format. From then on, adding/deleting rows to the range - or even moving the name to a completely different range, will automatically be reflected in the Word document. Yet another approach would be to use a DATABASE field in Word to capture the relevant Excel data. For a demonstration, see:
http://www.msofficeforums.com/mail-merge/21847-mail-merge-into-different-coloumns.html#post67097

As for mailmerge, you can use Word's Catalogue/Directory Mailmerge facility. To see how to do so with any mailmerge data source supported by Word, check out my Microsoft Word Catalogue/Directory Mailmerge Tutorial at:
http://windowssecrets.com/forums/showthread.php/154370-Microsoft-Word-Catalogue-Directory-Mailmerge-Tutorial
or:
http://www.gmayor.com/Zips/Catalogue%20Mailmerge.zip
The tutorial covers everything from list creation to the insertion & calculation of values in multi-record tables in letters. Do read the tutorial before trying to use the mailmerge document included with it.

Depending on what you're trying to achieve, the field coding for this can be complex. However, since the tutorial document includes working field codes for all of its examples, most of the hard work has already been done for you - you should be able to do little more than copy/paste the relevant field codes into your own mailmerge main document, substitute/insert your own field names and adjust the formatting to get the results you desire. For some worked examples, see the attachments to the posts at:
http://www.msofficeforums.com/mail-merge/9180-mail-merge-duplicate-names-but-different-dollar.html#post23345
http://www.msofficeforums.com/mail-merge/11436-access-word-creating-list-multiple-records.html#post30327