Word Nested Mail Merge in C#, VB.NET

  • Demo
  • C# source
  • VB.Net source

The sample demonstrates how to merge nestedmail into a document.


private void button1_Click(object sender, EventArgs e)
{
    List list = new List();
    DataSet dsData = new DataSet();

    dsData.ReadXml(@"..\..\..\..\..\..\Data\Orders.xml");

    //Create word document
    Document document = new Document();
    document.LoadFromFile(@"..\..\..\..\..\..\Data\Invoice.doc");

    DictionaryEntry dictionaryEntry = new DictionaryEntry("Customer", string.Empty);
    list.Add(dictionaryEntry);

    dictionaryEntry = new DictionaryEntry("Order", "Customer_Id = %Customer.Customer_Id%");
    list.Add(dictionaryEntry);

    document.MailMerge.ExecuteWidthNestedRegion(dsData, list);
  
    //Save doc file.
    document.SaveToFile("Sample.doc", FileFormat.Doc);

    //Launching the MS Word file.
    WordDocViewer("Sample.doc");
}

private void WordDocViewer(string fileName)
{
    try
    {
        System.Diagnostics.Process.Start(fileName);
    }
    catch { }
}

Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles button1.Click
	Dim list_Renamed As New List(Of DictionaryEntry)()
	Dim dsData As New DataSet()

	dsData.ReadXml("..\..\..\..\..\..\Data\Orders.xml")

	'Create word document
	Dim document_Renamed As New Document()
	document_Renamed.LoadFromFile("..\..\..\..\..\..\Data\Invoice.doc")

	Dim dictionaryEntry_Renamed As New DictionaryEntry("Customer", String.Empty)
	list_Renamed.Add(dictionaryEntry_Renamed)

	dictionaryEntry_Renamed = New DictionaryEntry("Order", "Customer_Id = %Customer.Customer_Id%")
	list_Renamed.Add(dictionaryEntry_Renamed)

	document_Renamed.MailMerge.ExecuteWidthNestedRegion(dsData, list_Renamed)

	'Save doc file.
	document_Renamed.SaveToFile("Sample.doc", FileFormat.Doc)

	'Launching the MS Word file.
	WordDocViewer("Sample.doc")
End Sub

Private Sub WordDocViewer(ByVal fileName As String)
	Try
		Process.Start(fileName)
	Catch
	End Try
End Sub