Share via

Inserting Formatted Table Via Mail Merge

Anonymous
2014-03-26T14:38:58+00:00

Hello everybody,

I'm not sure if this should go in the excel or word subforums since it involves both.  I am trying to create a number of documents that are largely identical with specific values inserted in the correct spots.  It seemed like a perfect situation for a mail merge.  The issue is that each document has a semi-specific table that I need to add a line to.  I've considered doing some sort of find and replace, but I don't think mail merge can take multiple existing documents as input.  I'm under the impression that it's a one template-> multiple documents features, not n templates -> n documents.

This has led me to think that what I need is to have a cell in excel that contains the formatted table, a cell with the new line, and a cell that joins the two.  Then each time, I would copy over the previous "final" table and change the added line to produce the new table.  I could then insert the formatted table into the word document.  The issue I'm having is that I can't get the mail merge to do this.  I tried pasting the table into the cell and pasting the html for the table, but I can't figure out how to insert a formatted table into word via mail merge. 

My table should look something like:

Year           Percent                 Reason

1995          0%                       First year

1996          2%                       Increase required due to...

Thank you for any help on this,

Note: I have posted this question on excelforum at http://www.excelforum.com/word-formatting-and-general/999680-inserting-formatted-table-via-mail-merge.html#post3637736

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Anonymous
2014-03-30T16:57:15+00:00

In my view, to produce this report in Merge, you would probably still be better off "flattening" all the data and using one of the methods that has already been suggested.

However, if you really need not to flatten tthe data to that extent, I have posted an example based on a different approach which inserts the rate history data using a DATABASE field, separating the data into two worksheets.

I hope you will be able to download the example from here: http://goo.gl/6Dkkxx

To see what it is doing, you will need to download the .zip (let me know if you cannot do that) and unzip the .docm and .xlsx files into a folder called c:\insrates. I have used a .docm because this approach will require you to run a small macro to format the rate history table post-merge. If you are not allowed to run macros, this approach is unlikely to work for you.

Then I suggest you open the .xlsx file, where you will see two sheets:

main, containing basic, current details like this

State    Form    Rate 1     Rate 2....  

IN         M3        $2,324    $2,541    

and detail, containing the rate history (actually more like a rate change history), that repeats the two key fields State and Form and has columns for Year, Rate (change), and Reason. There's a special row at the top of the rate table to deal with some problems Word has getting data from Excel. Don't delete it!

The .docm is a Mail Merge Main document, but it needs to be hooked up to its data source. Once done, you shouldn't need to do that again unless you move the files.

 To hook it up, you need to do two things. Leave the .xlsx open for the time being and open the .docm. ensure that field codes are displaying (so that you can see { MERGEFIELD Form }, for example). Don't attempt to update any fields at this point.

Then look through the document until you find a field that starts like this:

{ XDATABASE

Delete the "X", and if you have changed the location of the .xlsx, modify the file name to match. Backslashes need to be doubled up - e.g. c:\a\myfile.xlsx needs to be specified as c:\a\myfile.xlsx. and \\myserver\myshare\myfile.xlsx needs to be specified as \\myserver\myshare\myfile.xlsx

Notice that the DATABASE field has some SQL with a WHERE clause that contains some { MERGEFIELD } fields. This is so that you grab the history records associated with the State/Form pair currently being merged. Since we haven't attached the mailmerge data source yet, trying to update the DATABASE field's results should fail. You may also see that I have had to insert a bit of SQL to deal with the fact that you can have either percentages or text in your "Rate" column.

Now use Mailings->Select Recipients to locate the .xlsx as the data source for the merge. In the list of tables select the "main$" table.

If that works OK, I suggest you do a Save As to save the .docm immediately.

Then show the field code results via Alt-F9, and try previewing the merge. You should see the DATABASE table reult change as you change record.

If you now merge to an output document, the DATABASE fields should be replaced by the appropriate rate table. However, Word will use default column widths, and they almost certainly won't be suitable. You can try to fix this problem by modifying the DATABASE field so that it has a \*MERGEFORMAT switch at the end, but I do not believe that actually solves the problem, and the format is very easy to lose if you modify anything.

So I have provided a bit of VBA that goes through the output document and reformats the columns. It also right-justifies the rate column. At the moment, this macro is stored in the ThisDocument module of the .docm - I leave you to work out how to run it.

Assuming you have managed to follow all that, it really just leads me to some more questions.

  1. The main question is about the fact that you have a "formatted" table. Here, I have assumed that what you are really trying to do is avoid flattening the data "too much" and impose a consistent format, year-on-year, and that is what the formatting macro would attempt to do. But if for example you are marking up the resulting table (perhaps with some footnotes or comments), then this won't work. In fact, if you are doing that, I suspect that the only way you will achieve what you want is via a workflow where you do something like the following:

 a. ensure that the table layout is fixed (i.e. all column widths are fixed

 b. generate an initial merge document for each state/form combination

 c. use a macro to do a separate merge for each state/form

 d. next year, use a macro to copy the table generated the previous year into the mail merge main document for the state/form combination, ensuring that you have one row at the end of the table with the merge fields you need for the new year.

 e. repeat (c).

However, I suspect it would be simpler not to do a merge in that case. You would probably be better off having a macro that looked up the correct data for each state/form combination and inserted it.

  1. It wasn't completely clear to me whether you were producing one document per state/form combination, or one document per company/state/form combination (because "company" is mentioned at the beginning of your mail merge main document but nowhere in your Excel data.
  2. There are a few other things I've highlighted in my sample .docm.

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

20 additional answers

Sort by: Most helpful
  1. Anonymous
    2014-03-27T06:15:00+00:00

    That's the whole point of many to one. It creates a new row for each record related to the key field.

    Was this answer helpful?

    0 comments No comments
  2. Paul Edstein 82,861 Reputation points Volunteer Moderator
    2014-03-26T21:47:41+00:00

    The many-to one mailmerges available from:

    Graham Mayor at http://www.gmayor.com/ManyToOne.htm; and

    Doug Robbins at https://skydrive.live.com/?cid=5aedcb43615e886b#!/?cid=5AEDCB43615E886B!cid=5AEDCB43615E886B&id=5AEDCB43615E886B%21566

    are quite flexible and can easily handle variable numbers of rows per record. In addition to a 'Many to One' merge, the latter handles:

    • Merge with Charts

    • Duplex Merge

    • Merge with FormFields

    • Merge with Attachments

    • Merge to Individual Documents

    • Merge, Print and Staple

    There is also no particular reason you can't conditionally output variable, formatted text, via IF fields that test the value of a given mergefield. The variable data don't all need to be in the data source.

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2014-03-26T16:15:31+00:00

    Thanks Graham,

    I've considered a many-to-one mail merge, but the number of rows in the table is variable, both by record, and each time I run it.  If I don't have the entire table contained in one cell, I would need to to be dynamic.  I've also considered simply "flattening" the table into (columns x rows) number of entires for a single record, but have the same issue with the dynamic range. 

    Do you have any suggestion for either causing a single cell to contain all the formatting for a table in word, or for fixing the dynamic range issue?

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2014-03-26T15:30:30+00:00

    From your description, this sounds like a many to one mail merge, which Word cannot do out of the box, however see http://www.gmayor.com/ManyToOne.htm

    Was this answer helpful?

    0 comments No comments