Word Mail Merge from two data sources - ms-word

I have a bespoke system that produces two data sources (i.e. 2 csv files): One with basic header info. for clients and another with transactions for the clients. All in same file with unique client ref no.
Is it possible to set up the mail merge so that Word produces a letter per client with a table in the letter with transaction rows? The table with transactions will contain data from another data source (csv file) as mentioned above.
Thank you

If you save the transactions file as an Excel workbook, this is quite possible - provided the file with basic header info contains at least one field found in the transactions file. The solution requires some field coding (programming) to implement:
First, create a Word mailmerge main document and connect it to the file with the basic header info.
Second, create a DATABASE field in the mailmerge main document, coded along the lines of:
{DATABASE \d "{FILENAME \p}/../Transactions.xlsx" \s "SELECT [TransDate], [TransID], [TransAmt] FROM [Transactions$] WHERE [ClientID] = {MERGEFIELD ClientID} ORDER BY [InvoiceDate]" \l "9" \b "47" \h}
where 'ClientID' is a field found in both the basic header and the transactions file and 'Transactions' in [Transactions$] is the Sheet name.
Once you've done the setup correctly, your merge should be able to produce a letter per client with a table showing the relevant transactions.
A more detailed discussion of this approach can be found at:
https://answers.microsoft.com/en-us/msoffice/forum/msoffice_word-mso_winother-mso_2010/mail-merge-to-a-word-table-on-a-single-page/4edb4654-27e0-47d2-bd5f-8642e46fa103. For a working example, see:
http://www.msofficeforums.com/mail-merge/37844-mail-merge-using-one-excel-file-multiple.html
Note: The field brace pairs (i.e. '{ }') for the above example are all created in the document itself, via Ctrl-F9 (Cmd-F9 on a Mac or, if you’re using a laptop, you might need to use Ctrl-Fn-F9); you can't simply type them or copy & paste them from this message. Nor is it practical to add them via any of the standard Word dialogues. The spaces represented in the field constructions are all required.
For more info on the DATABASE field, see: https://support.office.com/en-us/article/field-codes-database-field-04398159-a2c9-463f-bb59-558a87badcbc?ocmsassetID=HP010260104&CorrelationId=13f3ddc8-95e9-4b61-a577-61e3f961c75d&ui=en-US&rs=en-US&ad=US

Related

Is it safe to change the data source format to OLE DB database file when using a htm/html as the datasource to remove the limitation of 62 fields?

I have a mail merge datasource which is in htm/html format and it contains 70 fields. Since there is a limitation of 62 fields for such datasources(Reference).
Is it safe to change the datasource type to OLE DB database file in the confirm datasource dialog when selecting the datasource?
When you choose the "All web pages" type (and this is the default type in the case of an HTML file), you are in essence choosing a Word internal file converter to retrieve your data. The reason you end up with the concatenated columns is because
The internal converter is not primarily designed to "read data sources". It's there to convert a document in HTML format into a document in Word format.
Your HTML file contains a table in HTML format, so naturally, the converter tries to convert that into a Word table
However, Word tables can only have 63 columns, whereas HTML tables can have more, so the converter has to deal with that somehow. In this case, it concatenates the column data so column 63 ends up containing all the remaining data in the row.
Once the document is converted, Word uses the converted document as the data source. It's really no different from the situation where it uses a Word document as the data source.
If your HTML file actually contained (say) 1 paragraph of 70 comma-delimited values for each row of data, rather than an HTML table row with td cells, Word would end up treating the data as 70 separate columns (but it would also probably ask for the column delimiter every time you used the file, and you would have to ensure that commas in the data were correctly quoted.
In general, when you choose the "OLE DB Database Files" option, Word either knows of an OLE DB Provider type that can read the specified type of file, or it won't be able to read the file. In this case, what it tries to do is read the file using the Jet OLE DB provider (or in recent versions of Word, the ACE OLE DB provider).
The Jet/ACE providers are one of the mechanisms used to read Access .mdb/.accdb data, but these providers can read a number of formats such as Excel workbook data and plain text file data, using a number of what Jet/ACE calls "Installable ISAMs (IISAMs).
Since there is an IISAM for HTML format data, Word will try to get the data using that IISAM.
In that case, as long as the IISAM can actually read the HTML (it may not be able to read more modern versions of HTML very well) it works much more like the case where Word gets data from Excel. For example, if your HTML file contained two tables, you may get to choose which table to read, cf. an Excel workbook with multiple worksheets and perhaps named ranges.
Jet/ACE IISAMs generally do not support more than 255 columns. 70 shhould be fine. However, you may need to verify what the HTML IISAM does about
Columns with mixed data types (for example where some rows have numbers in them and others have text). When the Excel IISAM finds such data in the first 8 rows (by default) it tries to choose a format - somtimes that can mean that cells with text are read as if they contained "0". FWIW I do not think the HTML IISAM does that, but I would check anyway.
Columns with large amount of text, particularly if there is more than one such column. The IISAM is quite likely to truncate such columns to 255 characters or even less.
Columns with non-ANSI data (non-ANSI Unicode text e.g. Arabic, Hindi or Chinese text.
Other than delimited text files which will let you go over theat 255 limit if they are read by the internal converter, the only data source I know that will let WOrd see thousands of columns is SQL Server. Other servers with OLE DB providers such as MySQL might allow that too. If you have to use a very large number of columns, be aware that you may not see all the available field names in the relevant dropdowns in WOrd, but you should be able to insert the MERGEFIELD codes in manually in the usual way.
What is your current mailmerge connection method (OLEDB, DDE)? By switching to the OLEDB connection method - which is Word's default - you would not be changing the datasource type (only the connection method). Whether doing so will work with your datasource can easily be established by changing to OLEDB and leaving the datasource type alone. If it doesn't work, close the document without saving (or revert to the current connection method.
Regardless, the screen you're showing allows you to specify a datasource type, not the connection method. HTML files are not OLE DB database files and you'd be unlikely to find your datasource if you switched to that file type.
In any event, the 62-field limitation most likely only relates to the fields you can see via the GUI. If you know the field name, you can insert its reference via the keyboard. To do so, simply press Ctrl-F9 to create a pair of field braces (i.e. { }) and fill in between them with 'MERGEFIELD' and the field name, thus { MERGEFIELD FieldName }.

Mail Merge multiple ID

I have data in excel, like below:
and, i have microsoft Word document, like below:
How to mail merge in microsoft word, one to Many row?
I want result like below in microsoft Word:
Thank You Very Much
This can be done, but it's a bit complex. There are a number of possible approaches, outlined at my website.
Since you show the desired result as a table, using a Database field is probably the optimal way to go about it. Insert Database is an old command that's no longer exposed in the Word UI by default. You'll find it in File/Options/Customize Ribbon or Quick Access Toolbar, under All Commands.
The command inserts a field with the name Database, via a set of dialog boxes:
Get Data is the same as what you see in mail merge when selecting the data source. This uses any valid connection method (these days, ODBC or OLE DB - the latter is the default) to bind to the data source. Select the data source containing the "many" information. (Note: the "one" side should be only the unique "one" information; the "many" side should be in a separate data source containing the unique identifier from the "one" side for each item on the "many" side.)
Query options is for setting Query Options (filter/sort what comes in). On the left side of the "equation" you need to select the field that is the identifier in the data source for the one side of one-to-many. On the right side, enter a value you know is in the data so that there's a match.
Table AutoFormat can be used to select a built-in (or user-defined) Table Style.
Insert Data - This is important: activate the checkbox Insert Data as field. This is what will dynamically link the data to the data source and provide a link to the merge information.
OK to insert the data / field.
Press Alt+F9 to view the underlying field codes.
Locate the query information (Select...) near the end of the field code. Change the right side of the Where clause to match the mergefield that provides the "one" side of one-to-many. For example: WHERE ((ID= 1)) would become WHERE ((ID= { Mergefield ID }))
If you don't want to see some of the fields (columns), such as an ID column (the "one" side of one-to-many), edit the list of fields at the beginning of the Select statement.
The result will look something like the following

Deleting substrings throughout a column

I am trying to clean up throughout columns within a table to create a clear attribution/reference for reporting on my digital marketing campaigns. The goal is to keep one part of a string while deleting all others. All strings within my marketing campaigns have symbols separating each substring.
Attached are pictures of my current table and of the desired table.
I am essentially trying to only keep on part of the structure of a string and delete all other sub strings. I have already managed to do this successfully by applying the following formula given to be from a separate thread.
update adwords
set campaign = substring(campaign from '%-%-#"%#"' for '#')
where campaign like '%-%-%';
This worked perfectly, however, I do not fully understand why and have not found a clear answer thus far on this forum.
How would I apply this to future rows? Ad group and match type can be used for this purpose.
Many Thanks.
First thing: You do not modify source data. Do ETL instead, and transform it to a final stage. Do that periodically and thus taking care of new data.
You could just create a trigger which should work for all new data, but there are 2 caveats with that:
Failure will lead to missing data and you not being able to QA it.
If you modify the source data in an incorrect way by mistake, you cannot undo it unless you have a backup, and even then it's just too hard.
So instead look at ETL tools like Talend or Pentaho Kettle; create your own ETL scripts, or whatever. Use Jenkins to schedule all of this periodically and you're set.
Now, about the transformation itself.
for '#'
indicates that # will be an escape symbol, which means that #" will be treated as a regular quote in this case.
substring(campaign from '%-%-#"%#"' for '#')
thus, selects everything between the quotes in the pattern. % is a wildcard, same as used in LIKE comparisons. So everything in the last group will be returned. This can better be done with regular expressions
substring(campaign from '.*?-.*?-(.*)')
For the second column the regex would be ^(.*?)\s*\{
And for the third one - similar: ^(.*?)\s*\}
I would create the new table like this:
CREATE TABLE aw_final AS
SELECT
substring(campaign FROM '^\w{2}-\w+-(.*)$') AS campaign,
substring(ad_group FROM '^(\w+)\s*\{\w+\}$') AS ad_group,
substring(match_type FROM '^(\w+)\s*\}$') AS match_type
FROM adwords
WHERE campaign ~ '^\w{2}-\w+-(.*)$'
But if you must do an update, this would be how:
UPDATE adwords SET
campaign = substring(campaign FROM '^\w{2}-\w+-(.*)$'),
ad_group = substring(ad_group FROM '^(\w+)\s*\{\w+\}$'),
match_type = substring(match_type FROM '^(\w+)\s*\}$')
WHERE campaign ~ '^\w{2}-\w+-(.*)$'

MS Word, Import Table with Query Condition Based on Merge Field

I'm creating a compliance mailing for my organization, the mailing will include merge fields that identify the office location, physician, and SiteId. The mailing will also include a table of information that is dependent upon the particular SiteId.
I'd like to use the import table function of MS word and set up a query that references a merged field (SiteId) so that the inserted tables populate the appropriate data for the particular site. I'm unable to do this.
How can I set up this document so that I can import only records from my source (an ms access query) that match the SiteId merge field?
Word's mail merge does not support one-to-many relationships. There are ways to coerce it, but only one of them can yield a table as a result and over the years it has become less and less reliable as Microsoft has not regarded it as important enough to maintain...
What you need to do is set up a query that provides ONLY the information you want displayed in the table, plus the key (SiteId). It's best to sort it so that all the SiteId entries list together, and are in the order the data will come through in the mail merge data source.
On the Insert tab go to Text/Quick Parts/Insert Field and select the Database field from the list in the dialog box. Click "Insert Database" and follow the instructions in the dialog box to link in the data. Be sure to set the Query Options to filter on the first SiteId from the data source. When you "Insert Data" make sure to choose the option to "Insert as a field".
This inserts a DATABASE field in the document which you can see by toggling field codes (Alt+F9). The field code can be edited and what you need to do is substitute the literal SiteId value you entered for the query with its corresponding MergeField.
When you execute the merge to a new document that should generate a table for each data record corresponding to the SiteId for the record. But, as I said, Microsoft hasn't done a great job of maintaining this, so it may require quite a bit of tweaking and experimenting.
If the results are not satisfactory then you should give up the idea of mail merge and use automation code to generate and populate the documents.
You can find more (albeit somewhat out-dated) information on this topic at http://homepage.swissonline.ch/cindymeister/mergfaq1.htm

Word Mail Merge Fields

I have not worked with Mail Merge fields before and everything I find requires you to select a data source prior to be able to insert merge fields. All I want to do is place fields on the word document and not merge it down until its consumed by the code. I essentially am creating document templates. How is this done in word?
As Noah said, it is probably easiest to set up a dummy data source containing the available fields, especially if you haven't worked with Mail Merge before.
However, Word fields can be created completed manually. Press Alt+I and then F (on an English Word) to open the Insert Fields dialog (For Word XP/2003 you will find the same in the Insert menu, for Word 2007 the dialog is available from the Insert tab of the ribbon under Quick Parts). In this dialog you can select the mail merge fields and specify all relevant options, e.g. a field of type MergeField.
If you know the field code (and the relevant options) you can also insert the field directly by pressing Ctrl+F9 followed by the field code (Note that you can toggle the display of field codes using Alt+F9). This is a very convenient - albeit advanced - way when creating templates.
I would follow this Microsoft article. If you don't have the data source available, I would simply create a dummy Excel/Access/data source with the fields that you will have. Word prefers to know what fields are available and you can change the source once your template document is built.