DocumentMailMergeSettings Property
Gets or sets the object that contains all of the mail merge information for a document.

Namespace: Aspose.Words
Assembly: Aspose.Words (in Aspose.Words.dll) Version: 19.9
public MailMergeSettings MailMergeSettings { get; set; }

Property Value

Type: MailMergeSettings

You can use this object to specify a mail merge data source for a document and this information (along with the available data fields) will appear in Microsoft Word when the user opens this document. Or you can use this object to query mail merge settings that the user has specified in Microsoft Word for this document.

This object is never null.

Shows how to execute a mail merge with MailMergeSettings.

// We'll create a simple document that will act as a destination for mail merge data
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.Write("Dear ");
builder.InsertField("MERGEFIELD FirstName", "<FirstName>");
builder.Write(" ");
builder.InsertField("MERGEFIELD LastName", "<LastName>");
builder.Writeln(": ");
builder.InsertField("MERGEFIELD Message", "<Message>");

// Also we'll need a data source, in this case it will be an ASCII text file
// We can use any character we want as a delimiter, in this case we'll choose '|'
// The delimiter character is selected in the ODSO settings of mail merge settings
string[] lines = { "FirstName|LastName|Message",
    "John|Doe|Hello! This message was created with Aspose Words mail merge." };
File.WriteAllLines(ArtifactsDir + "Document.Lines.txt", lines);

// Set the data source, query and other things
MailMergeSettings mailMergeSettings = doc.MailMergeSettings;
mailMergeSettings.MainDocumentType = MailMergeMainDocumentType.MailingLabels;
mailMergeSettings.DataType = MailMergeDataType.Native;
mailMergeSettings.DataSource = ArtifactsDir + "Document.Lines.txt";
mailMergeSettings.Query = "SELECT * FROM " + doc.MailMergeSettings.DataSource;
mailMergeSettings.LinkToQuery = true;
mailMergeSettings.ViewMergedData = true;

// Office Data Source Object settings
Odso odso = mailMergeSettings.Odso;
odso.DataSourceType = OdsoDataSourceType.Text;
odso.ColumnDelimiter = '|';
odso.DataSource = ArtifactsDir + "Document.Lines.txt";
odso.FirstRowContainsColumnNames = true;

// The mail merge will be performed when this document is opened 
doc.Save(ArtifactsDir + "Document.MailMergeSettings.docx");
See Also