Mail Merge is the feature which allows to easy generate documents populated by data using a template.
To illustrate how works Mail Merge function, let's create C# application which generates envelopes "Happy New Year" for Simpson family. As result we'll get five personal Happy New Year greetings in a single document.
To start, open MS Word and create a new document with the name "envelope-template.docx". Next add some
images and a text to congratulate the Simpson's family. Next, is our main goal - add two Fields:
"Name" and "FamilyName".
If you are already familiar with adding of Merge
Fields, you may skip this step and use completely ready «envelope-template.docx»
Here you may an extra information how to Insert Merge Fields
As result of the Step 1 we've to get: envelope-template.docx
You may add the reference to the SautinSoft.Document assembly by two ways:
1. Nuget (fast way):
2. Old good way by adding the reference:
Note:
SautinSoft.Document.dll compiled for .NET Core is located inside (document_net.zip->Document .Net X.X\Bin\.NET Core X.X) folder.
SautinSoft.Document.dll compiled for .NET Framework is located inside (document_net.zip->Document .Net X.X\Bin\.NET Framework X.X) folder.
Type the code of our App: (We've also prepared the code fand .NET developers)
Complete code
using System;
using System.IO;
using SautinSoft.Document;
namespace Sample
{
class Sample
{
static void Main(string[] args)
{
// Get your free 30-day key here:
// https://sautinsoft.com/start-for-free/
MailMergeSimpleEnvelope();
}
/// <summary>
/// Generates 5 envelopes "Happy New Year" for Simpson family using the one template.
/// </summary>
/// <remarks>
/// See details at: https://sautinsoft.com/products/document/help/net/developer-guide/mail-merge-simple-report-net-csharp-vb.php
/// </remarks>
public static void MailMergeSimpleEnvelope()
{
string templatePath = @"..\..\..\envelope-template.docx";
string resultPath = "Simpson-family.docx";
DocumentCore dc = DocumentCore.Load(templatePath);
var dataSource = new[] { new { Name = "Homer", FamilyName = "Simpson" },
new { Name = "Marge ", FamilyName = "Simpson" },
new { Name = "Bart", FamilyName = "Simpson" },
new { Name = "Lisa", FamilyName = "Simpson" },
new { Name = "Maggie", FamilyName = "Simpson" }};
dc.MailMerge.Execute(dataSource);
dc.Save(resultPath);
// Open the result for demonstration purposes.
System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(resultPath) { UseShellExecute = true });
}
}
}
Option Infer On
Imports System
Imports System.IO
Imports SautinSoft.Document
Module Sample
Sub Main()
MailMergeSimpleEnvelope()
End Sub
''' Get your free 30-day key here:
''' https://sautinsoft.com/start-for-free/
''' <summary>
''' Generates 5 envelopes "Happy New Year" for Simpson family using the one template.
''' </summary>
''' <remarks>
''' See details at: https://sautinsoft.com/products/document/help/net/developer-guide/mail-merge-simple-report-net-csharp-vb.php
''' </remarks>
Sub MailMergeSimpleEnvelope()
Dim templatePath As String = "..\..\..\envelope-template.docx"
Dim resultPath As String = "Simpson-family.docx"
Dim dc As DocumentCore = DocumentCore.Load(templatePath)
Dim dataSource = {
New With {
Key .Name = "Homer",
Key .FamilyName = "Simpson"
},
New With {
Key .Name = "Marge ",
Key .FamilyName = "Simpson"
},
New With {
Key .Name = "Bart",
Key .FamilyName = "Simpson"
},
New With {
Key .Name = "Lisa",
Key .FamilyName = "Simpson"
},
New With {
Key .Name = "Maggie",
Key .FamilyName = "Simpson"
}
}
dc.MailMerge.Execute(dataSource)
dc.Save(resultPath)
' Open the result for demonstration purposes.
System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(resultPath) With {.UseShellExecute = True})
End Sub
End Module
After execution of mail merge method we'll get the «simpson-family.docx» as result.
If you need a new code example or have a question: email us at support@sautinsoft.com or ask at Online Chat (right-bottom corner of this page) or use the Form below: