We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Mail Merging an Object

I’m currently trying out your product since it is something we are considering to use to further our Word Automation. We currently use this to send personlised letters, or to generate invoices for billing.

What i’d like to see (and i find it hard to believe this isn’t already implemented) is beeing able to Execute the MailMerge and add an Object, through reflection the object would then be merged, Fieldname would have to be equal to the property name but in my opinion that would make it very easy to MailMerge an Invoice object to InvoiceTemplate.doc.

I understand there is an interface for a MailMergeDataSource which makes sense when it comes to (for example) an InvoiceLine collection. To me this seems like it should be default functionality, i atleast would not like to have my domain objects beeing dependant on third party software.

Is this like i am expecting default behavior that i simply haven’t been able to find yet? Or is this something aspose.words currently lacks?

Greetings,

Folkert


This message was posted using Page2Forum from Simple Mail Merge Explained - Aspose.Words for .NET and Java

Hi

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your inquiry. You are right, you can easily achieve what you need using IMailMergeDataSource. For example you can try using the following implementation:

///

/// A custom mail merge data source that you implement to allow Aspose.Words

/// to mail merge data from LINQ query results into Microsoft Word documents.

///

public class MailMergeDataSource : IMailMergeDataSource

{

///

/// Creates new instance of a custom mail merge data source

///

/// Data returned from a LINQ query

public MailMergeDataSource(IEnumerable data)

{

mEnumerator = data.GetEnumerator();

}

///

/// Creates new instance of a custom mail merge data source

///

/// Data returned from a LINQ query

/// Name of the data source is only used when you perform mail merge with regions.

/// If you would like to use simple mail merge then use constructor with one parameter.

public MailMergeDataSource(IEnumerable data, string tableName)

{

mEnumerator = data.GetEnumerator();

// Name of the data source is needed when you perform mail merge with regions

mTableName = tableName;

}

///

/// Aspose.Words call this to get a value for every data field.

///

public bool GetValue(string fieldName, out object fieldValue)

{

// Get type of current record

Type curentRecordType = mCurrentObject.GetType();

// Use reflection to get property by name and its value

PropertyInfo property = curentRecordType.GetProperty(fieldName);

if (property != null)

{

fieldValue = property.GetValue(mCurrentObject, null).ToString();

return true;

}

else

{

// A field with this name was not found,

// return false to the Aspose.Words mail merge engine.

fieldValue = null;

return false;

}

}

///

/// Moves to the next record in the collection.

///

public bool MoveNext()

{

// Move enumerator to next record

bool hasNexeRecord = mEnumerator.MoveNext();

if(hasNexeRecord)

{

mCurrentObject = mEnumerator.Current;

}

return hasNexeRecord;

}

///

/// The name of the data source. Used by Aspose.Words only when executing mail merge with repeatable regions.

///

public string TableName

{

get { return mTableName; }

}

private IEnumerator mEnumerator;

private object mCurrentObject;

private string mTableName = string.Empty;

}

Hope this helps. Please let me know if you need more assistance, I will be glad to help you.

Best regards.