Search code examples
c#mailmerge

MailMerge from arrays or lists


In this site is an example about doing a mailmerge with 2 arrays and the MailMerge.Execute method but the method don't accept 2 arguments.

Anyway, Is there a way of doing something similar with 2 arrays or 2 lists?

Thanks.

This is the code they use:

    // Open an existing document.
    Document doc = new Document(MyDir + "MailMerge.ExecuteArray.doc");

    // Fill the fields in the document with user data.
    doc.MailMerge.Execute(
    new string[] {"FullName", "Company", "Address", "Address2", "City"},
    new object[] {"James Bond", "MI5 Headquarters", "Milbank", "", "London"});

And this is the method I want to implement:

    private void ReplaceMailMergeField(String plantilla, String[] campos, Object[] valores)
    {
        Object oMissing = System.Reflection.Missing.Value;
        Object oTrue = true;
        Object oFalse = false;
        Word.Application wordApp = new Word.Application();
        Word.Document wordDoc = new Word.Document();
        wordApp.Visible = true;
        Object templatePath = plantilla;
        wordDoc = wordApp.Documents.Add(ref templatePath, ref oMissing, ref oMissing, ref oMissing);
        wordDoc.MailMerge.Execute(campos, valores);
    }

Solution

  • The first block of code that takes two arrays is Aspose Words for .NET, this is a separate component from the MS Word API.

    The MS Word MailMerge.Execute() is a different method and takes only one optional parameter.

    To automate mail merge with just C# and office installed see these articles:

    KB301659 How to automate Microsoft Word to perform Mail Merge from Visual C#

    http://everythingsharepoint.blogspot.in/2011/12/c-microsoft-word-2010-automated-mail.html

    http://everythingsharepoint.blogspot.in/2011/12/c-microsoft-word-2010-automated-mail_01.html