Implement this interface to allow mail merge from a custom data source, such as list of objects.
For a list of all members of this type, see IMailMergeDataSource Members.
[Visual Basic]
Public Interface IMailMergeDataSource
[C#]public interface IMailMergeDataSource
Remarks
Example
Performs mail merge from a custom data source.
[C#]
public void MailMergeCustomDataSource()
{
// Create some data that we will use in the mail merge.
CustomerList customers = new CustomerList();
customers.Add(new Customer("Thomas Hardy", "120 Hanover Sq., London"));
customers.Add(new Customer("Paolo Accorti", "Via Monte Bianco 34, Torino"));
// Open the template document.
Document doc = new Document(MyDir + "MailMerge.CustomDataSource.doc");
// To be able to mail merge from your own data source, it must be wrapped
// into an object that implements the IMailMergeDataSource interface.
CustomerMailMergeDataSource customersDataSource = new CustomerMailMergeDataSource(customers);
// Now you can pass your data source into Aspose.Words.
doc.MailMerge.Execute(customersDataSource);
doc.Save(MyDir + "MailMerge.CustomDataSource Out.doc");
}
/// <summary>
/// An example of a "data entity" class in your application.
/// </summary>
public class Customer
{
public Customer(string aFullName, string anAddress)
{
mFullName = aFullName;
mAddress = anAddress;
}
public string FullName
{
get { return mFullName; }
set { mFullName = value; }
}
public string Address
{
get { return mAddress; }
set { mAddress = value; }
}
private string mFullName;
private string mAddress;
}
/// <summary>
/// An example of a typed collection that contains your "data" objects.
/// </summary>
public class CustomerList : ArrayList
{
public new Customer this[int index]
{
get { return (Customer)base[index]; }
set { base[index] = value; }
}
}
/// <summary>
/// A custom mail merge data source that you implement to allow Aspose.Words
/// to mail merge data from your Customer objects into Microsoft Word documents.
/// </summary>
public class CustomerMailMergeDataSource : IMailMergeDataSource
{
public CustomerMailMergeDataSource(CustomerList customers)
{
mCustomers = customers;
// When the data source is initialized, it must be positioned before the first record.
mRecordIndex= -1;
}
/// <summary>
/// The name of the data source. Used by Aspose.Words only when executing mail merge with repeatable regions.
/// </summary>
public string TableName
{
get { return "Customer"; }
}
/// <summary>
/// Aspose.Words call this to get a value for every data field.
/// </summary>
public bool GetValue(string fieldName, out object fieldValue)
{
switch (fieldName)
{
case "FullName":
fieldValue = mCustomers[mRecordIndex].FullName;
return true;
case "Address":
fieldValue = mCustomers[mRecordIndex].Address;
return true;
default:
// A field with this name was not found,
// return false to the Aspose.Words mail merge engine.
fieldValue = null;
return false;
}
}
/// <summary>
/// A standard implementation for moving to a next record in a collection.
/// </summary>
public bool MoveNext()
{
if (!IsEof)
mRecordIndex++;
return (!IsEof);
}
private bool IsEof
{
get { return (mRecordIndex >= mCustomers.Count); }
}
private readonly CustomerList mCustomers;
private int mRecordIndex;
}[Visual Basic]
Public Sub MailMergeCustomDataSource()
' Create some data that we will use in the mail merge.
Dim customers As CustomerList = New CustomerList()
customers.Add(New Customer("Thomas Hardy", "120 Hanover Sq., London"))
customers.Add(New Customer("Paolo Accorti", "Via Monte Bianco 34, Torino"))
' Open the template document.
Dim doc As Document = New Document(MyDir & "MailMerge.CustomDataSource.doc")
' To be able to mail merge from your own data source, it must be wrapped
' into an object that implements the IMailMergeDataSource interface.
Dim customersDataSource As CustomerMailMergeDataSource = New CustomerMailMergeDataSource(customers)
' Now you can pass your data source into Aspose.Words.
doc.MailMerge.Execute(customersDataSource)
doc.Save(MyDir & "MailMerge.CustomDataSource Out.doc")
End Sub
''' <summary>
''' An example of a "data entity" class in your application.
''' </summary>
Public Class Customer
Public Sub New(ByVal aFullName As String, ByVal anAddress As String)
mFullName = aFullName
mAddress = anAddress
End Sub
Public Property FullName() As String
Get
Return mFullName
End Get
Set
mFullName = Value
End Set
End Property
Public Property Address() As String
Get
Return mAddress
End Get
Set
mAddress = Value
End Set
End Property
Private mFullName As String
Private mAddress As String
End Class
''' <summary>
''' An example of a typed collection that contains your "data" objects.
''' </summary>
Public Class CustomerList
Inherits ArrayList
Public Shadows Default Property Item(ByVal index As Integer) As Customer
Get
Return CType(MyBase.Item(index), Customer)
End Get
Set
MyBase.Item(index) = Value
End Set
End Property
End Class
''' <summary>
''' A custom mail merge data source that you implement to allow Aspose.Words
''' to mail merge data from your Customer objects into Microsoft Word documents.
''' </summary>
Public Class CustomerMailMergeDataSource
Implements IMailMergeDataSource
Public Sub New(ByVal customers As CustomerList)
mCustomers = customers
' When the data source is initialized, it must be positioned before the first record.
mRecordIndex= -1
End Sub
''' <summary>
''' The name of the data source. Used by Aspose.Words only when executing mail merge with repeatable regions.
''' </summary>
Public ReadOnly Property TableName() As String Implements IMailMergeDataSource.TableName
Get
Return "Customer"
End Get
End Property
''' <summary>
''' Aspose.Words call this to get a value for every data field.
''' </summary>
Public Function GetValue(ByVal fieldName As String, <System.Runtime.InteropServices.Out()> ByRef fieldValue As Object) As Boolean Implements IMailMergeDataSource.GetValue
Select Case fieldName
Case "FullName"
fieldValue = mCustomers(mRecordIndex).FullName
Return True
Case "Address"
fieldValue = mCustomers(mRecordIndex).Address
Return True
Case Else
' A field with this name was not found,
' return false to the Aspose.Words mail merge engine.
fieldValue = Nothing
Return False
End Select
End Function
''' <summary>
''' A standard implementation for moving to a next record in a collection.
''' </summary>
Public Function MoveNext() As Boolean Implements IMailMergeDataSource.MoveNext
If (Not IsEof) Then
mRecordIndex += 1
End If
Return ((Not IsEof))
End Function
Private ReadOnly Property IsEof() As Boolean
Get
Return (mRecordIndex >= mCustomers.Count)
End Get
End Property
Private ReadOnly mCustomers As CustomerList
Private mRecordIndex As Integer
End ClassRequirements
Namespace: Aspose.Words.Reporting
Assembly: Aspose.Words (in Aspose.Words.dll)
See Also
IMailMergeDataSource Members | Aspose.Words.Reporting Namespace | MailMerge