Simple Mail Merge Operation
A simple Mail Merge is used to fill the Mail Merge fields inside your template with the required data from your data source (single table representation). So it is similar to the classic Mail Merge in Microsoft Word.
You can add one or more merge fields in your template and then execute the simple Mail Merge operation. It is recommended to use it if your template does not contain any merge regions.
The main limitation of using this type is the whole document content will be repeated for each record in the data source.
How to Execute a Simple Mail Merge Operation
Once your template is ready, you can start performing the simple Mail Merge operation. Aspose.Words allows you to execute a simple Mail Merge operation using different Execute methods that accept various data objects as the data source.
The following code example shows how to execute a simple Mail Merge operation using one of the Execute method:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C.git. | |
auto doc = MakeObject<Document>(); | |
auto builder = MakeObject<DocumentBuilder>(doc); | |
builder->InsertField(u" MERGEFIELD CustomerName "); | |
builder->InsertParagraph(); | |
builder->InsertField(u" MERGEFIELD Item "); | |
builder->InsertParagraph(); | |
builder->InsertField(u" MERGEFIELD Quantity "); | |
auto boxHelper = [](const char16_t* value) { return System::ObjectExt::Box<String>(value); }; | |
// Fill the fields in the document with user data. | |
doc->get_MailMerge()->Execute( | |
MakeArray<String>({u"CustomerName", u"Item", u"Quantity"}), | |
MakeArray<SharedPtr<System::Object>>( | |
{ boxHelper(u"John Doe"), boxHelper(u"Hawaiian"), boxHelper(u"2")})); | |
doc->Save(ArtifactsDir + u"BaseOperations.SimpleMailMerge.docx"); |
You can notice the difference between the document before executing simple mail merge:

And after executing simple mail merge:

How to Create Multiple Merged Documents
In Aspose.Words, the standard Mail Merge operation fills only a single document with content from your data source. So, you will need to execute the Mail Merge operation many times to create a few merged documents as an output.
The following code example shows how to generate a few merged documents during a Mail Merge operation with SQLiteCpp:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C.git. | |
void MultipleDocumentsMailMerge() | |
{ | |
auto doc = MakeObject<Document>(); | |
auto builder = MakeObject<DocumentBuilder>(doc); | |
builder->InsertField(u" MERGEFIELD FullName "); | |
builder->InsertParagraph(); | |
builder->InsertField(u" MERGEFIELD Address "); | |
// Fill the fields in the document with user data. | |
SQLite::Database database{ (std::string)(DatabaseDir + u"customers.db3") }; | |
SQLite::Statement query{ database, "SELECT * FROM Customers" }; | |
auto dataSource = MakeObject<CustomersRowMailMergeDataSource>(query); | |
int32_t counter = 1; | |
while (query.executeStep()) | |
{ | |
dataSource->Reset(); | |
doc->get_MailMerge()->Execute(dataSource); | |
doc->Save(ArtifactsDir + u"BaseOperations.MultipleDocumentsMailMerge_" + counter++ + u".docx"); | |
} | |
} | |
class CustomersRowMailMergeDataSource : public MailMerging::IMailMergeDataSource | |
{ | |
public: | |
CustomersRowMailMergeDataSource(SQLite::Statement& query) : mQuery(query) {} | |
String get_TableName() override | |
{ | |
return u"Customers"; | |
} | |
bool GetValue(String fieldName, SharedPtr<Object>& fieldValue) override | |
{ | |
auto boxHelper = [](const std::string& value) { return System::ObjectExt::Box<String>(String::FromUtf8(value)); }; | |
if (fieldName == u"FullName") | |
{ | |
fieldValue = boxHelper(mQuery.getColumn(1).getString()); | |
return true; | |
} | |
if (fieldName == u"Address") | |
{ | |
fieldValue = boxHelper(mQuery.getColumn(2).getString()); | |
return true; | |
} | |
fieldValue.reset(); | |
return false; | |
} | |
bool MoveNext() override | |
{ | |
if (!isInitialized) | |
{ | |
isInitialized = true; | |
return true; | |
} | |
return false; | |
} | |
SharedPtr<IMailMergeDataSource> GetChildDataSource(String tableName) override | |
{ | |
return nullptr; | |
} | |
void Reset() | |
{ | |
isInitialized = false; | |
} | |
private: | |
SQLite::Statement& mQuery; | |
bool isInitialized = false; | |
}; |