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.


7 Responses to “C#: Word Mail Merge Bypass Header Record Delimiters Popup”

  1. Skyler Says:

    nice nice. sometimes the simplest solutions are the best; even if we don’t think of them at first.

    is the interaction with word streamlined with .NET? or did you have to do some funky stuff for it?

  2. Nick Olsen Says:

    Interaction is seamless. The only somewhat wierd thing is that the API methods accept a grip of parameters that you usually don’t use. For the parameters you don’t use you have to pass a reference to a special “Missing” value. Not hard by any means, just makes the code look a bit strange at first. Here is the code for the mail merge in C#.

    private void Merge(string fileName, string[] tableHeader, List formLetterDatas)
    Word.Selection wordSelection;
    Word.MailMerge wordMailMerge;

    // Create an instance of Word and make it visible.
    _wordApp = new Word.Application();
    _wordApp.Visible = true;
    _wordDoc = new Word.Document();

    // Add a new document.
    Object ofileName = fileName;
    _wordDoc = _wordApp.Documents.Open(ref ofileName, ref oMissing, ref oTrue, ref oMissing,
    ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
    ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);


    wordSelection = _wordApp.Selection;
    wordMailMerge = _wordDoc.MailMerge;

    // Create a MailMerge Data file.
    CreateMailMergeDataFile(tableHeader, formLetterDatas);

    // Open the MailMerge Data file
    object oSubType = Word.WdMergeSubType.wdMergeSubTypeOther;
    wordMailMerge.OpenDataSource(Constants.FormLetterDataFile, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
    ref oFalse, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
    ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oSubType);

    // Perform mail merge.
    wordMailMerge.Destination = Word.WdMailMergeDestination.wdSendToNewDocument;

    wordMailMerge.Execute(ref oFalse);

    // Close the original form document.
    _wordDoc.Saved = true;
    _wordDoc.Close(ref oFalse, ref oMissing, ref oMissing);

    // Release References.
    wordSelection = null;
    wordMailMerge = null;
    _wordDoc = null;
    _wordApp = null;



  3. ecommerce solution Says:

    Thanks for sharing the code. I would like to implement it.
    Keep posting!

  4. Lisa Greenwood Says:

    I am not working in C#, but I have the exact problem. Only difference is I am working in VB and I already have 500+ fields coming in and I cannot get rid of the pop up box asking about the delimiters (using ‘|’ for fields and ‘~’ for records). Any other recommendations?

    • Nick Olsen Says:

      I went back and forth with the Microsoft Office team on this and from what they said their is no way to specify the delimiters via code. Is it possible for you to use a tab for the field delimiters and a carriage return for the records? That is going to be your best bet. If you aren’t getting the file in that way, could you just do a String.Replace call replacing ‘|’ with ‘\t’ and ‘~’ with ‘\r’?

  5. asif Says:

    How would you specify a delimiter when the datasource is a sql command. I have word 2010, sql server 2008 and trying to a mailmerge and getting this message. My data will always be one record as it is “select * from letters where id=” & IDnumber.

    there are multiple fields (lots actually, hence the * as they would be more than 510 chars long if listed out), but i still get the message.

    so in summary, i am getting the message when there are multiple fields but just a single record.

    any ideas?

    • Nick Olsen Says:

      I have never done a mail merge with SQL Server before but if you are getting the same message then it sounds like Word cannot figure out that there are multiple fields in the record set. Maybe try including a tab or a comma or something in between each field in your select statement. That may not work as you are selecting a large number of fields though…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: