C#: Word Mail Merge Bypass Header Record Delimiters Popup


One of the applications that I have written allows the user to create custom form letters in Microsoft Word. The user is able to create a Word document and populate it with mail merge fields and then I programmatically create the mail merge data source and perform the merge. Everything worked fantastic except when the user created a form letter that only contained one merge field. Under this circumstance, every time they attempted to perform the merge from within my application, Microsoft Word would pop up with the Header Record Delimiters window, shown below, asking the user to specify how the fields and the records were delimited.

With more than one field, Microsoft Word was able to automatically detect the tab that was inserted between fields and use it as the delimiter but with only one field, it could not. The solution to this problem is actually so simple that I felt a bit retarded that I didn’t think of it earlier. All you have to do is add another merge field in your data file so that there is never only one merge field! The second column of data in your merge file will not correspond to any merge field in the document but Word doesn’t complain about it and it is able to automatically detect the tab between fields.

So, if you only have one merge field, just setup your data file to look something like the following:

MergeField1 [DUMMY_FIELD]
Data1 NULL
Data2 NULL
Data3 NULL

Note: You obviously can put whatever you want for the data in the DUMMY_FIELD column. I just put the string NULL for kicks.

Follow

Get every new post delivered to your Inbox.

Join 68 other followers