Template creation in Microsoft Word 2003

A core part of any LiveDocx project is the creation of a template file. Using Microsoft Word 2003, this takes only a matter of minutes:

  1. Open Microsoft Word 2003 to create a new document.
  2. Select Insert, Field… from the main menu to open the Field dialog box.
  3. Choose the MergeField from the Mail Merge category and specify a name for the new field: name.
  4. Specify the format and some field options as shown in the following screenshot:
    MS Word field options
  5. Repeat step 2 and 3 to insert another field and name this field “company”. Additionally, you could change the text of both fields. The finished template should now look like this:
    Template

Consider the following C# code, which can be used to populate the template created above. Assume that the template has been saved with the filename template.docx:

try
{
 LiveDocx.MailMerge mailMerge1 = new LiveDocx.MailMerge();
 CookieContainer cookieJar = new CookieContainer();
 mailMerge1.CookieContainer = cookieJar;

 mailMerge1.LogIn("username", "password");
 mailMerge1.SetRemoteTemplate("template.docx");

 string[][] mergeData = new string[][] {
   new string[] { "name", "company" },
   new string[] { "Mitchel", "Flowers Direct 2000" } };

 mailMerge1.SetFieldValues(mergeData);

 mailMerge1.CreateDocument();

 byte[] doc =
   Convert.FromBase64String(mailMerge1.RetrieveDocument("PDF"));

 File.WriteAllBytes("c:\\test" + Guid.NewGuid().ToString() + ".pdf", doc);
}
catch (Exception exc)
{
 Console.WriteLine(exc.Message);
}

LiveDocx Documentation