Word Mail Merge acting odd with crlf

I’ve just found a rather strange behaviour in Word 2010 Mail Merge. I’ve inherited the maintenance of a C# program that was generating letters by sending data from SQL Server to an Access mdb file, then using that as the data source. This is a pain to manage so I decided to change it to write to a csv file, and use that as the source. It all looked like it was working, but I ran into a very odd problem with fields containing new lines (crlf). I wrapped these fields in ” marks, so that the lines came in to the document OK as separate paragraphs, but unlike when imported from Access they had paragraph spacing between them. So from Access it would have

Line 1.¶
Line 2.¶

but from csv it would look like

Line 1.¶

Line 2.¶

After some trial and error I finally got the same result from csv as from Access by removing the lf characters. Presumably the Access source only sends through cr, even though it has crlf stored in the field.

Edit:

Of course, it turned out to be more complicated than that. It’s MS Word, it always does. If the field is in a table, the change I made will display the new lines as square characters. The fix for that is to check whether the string contains new lines (when writing the csv, of course) change them to crs as before, but then make sure there is a crlf on the end.

Leave a Reply

Your email address will not be published. Required fields are marked *


The reCAPTCHA verification period has expired. Please reload the page.