Formatting merge field value


#1

Hi Roman,

How can I change formatting of merge fields? For example, my data table contains
a column that indicates a product price … how can I show that price like $10.23? The
underlying column data type is numeric (System.Double that is). I tried hooking up into
the event system but that didn’t work.

This is a sample of code I used:

If (e.FieldName = “price”) then
e.Text = String.Format("{0:C}", e.FieldValue)
end if

This template is using TableStart, TableEnd constructs as there are multiple rows to show.

Thanks.
Abdim


#2

Hi Abdim,

So what do you get in the document? The approach you are using is the right one and it all should be fine. I’ll check my tests here.

Alternative would be to create a calculated column that contains formatted data, but it is only for the last resort.


#3

Hi Abdim,

1. You compare strings using “==” that means I think it does case sensitive compare so make sure name of the field is spelled exactly like in the document.

2. I confirm that when using object array as a data source there is a problem that all values are converted to strings before event handler is invoked. Therefore if you use Execute() that accepts object[] as values they will not format as currency because they are already strings by the time you receive the values in the event handler. I’ve fixed this and it will be out in the next release. If you need an urgent workaround and you are using object array as a data source then convert the value into double before formatting:

if (e.FieldName == “price”)
{
double price = double.Parse(e.FieldValue);
e.Text = price.ToString(“C”);
}


#4

Hi Roman,

That helped … Thanks.

Let me explain what I am doing, perhaps you can shed some more light or
add a feature or two… bunch of tables (instances of DataTable that is)
are created dynamically and that data is fed to MailMerge object. Depending
on the type of the table (global or not) Execute or ExecuteWithRegions is invoked.
The data is retrieved from Oracle and formatting on the backend is akward so I would
like to do the formatting at the merge time. So in the event handler I need to
look up the format picture for that field and if one exists apply it. However, given
the fact that multiple tables are involved in the merge operation, how can I effectively
perform the lookup without incurring performance penalty. Then I thought, perhaps,
you can have a property SourceColumn in MergeFieldEventArgs that would give
me an access to the underlying column feeding the current merge operation. This
way I would know the table currently used, and by way of DataColumn.ExtendedProperties
I could apply appropriate formatting or what not. Sure SourceColumn would be null
when not applicable. What do you think? Or is there a better way?

Regards,
Abdim


#5

Hi Abdim,

I understand what you are trying to achieve. I would prefer to add just TableName property to event arguments instead of SourceColumn object - this should give enough context. I’ll add this to the next release.

Alternative solution - we are in OO world so it is easy to find another place to store context information. Your mail merge event handler is a method of some class. Add TableName or DataTable property to that class and set it to the table you are currently processing before invoking mail merge - this way you will know what table you are in.


#6

Hi Roman,

TableName property will do. Is the next release in May?

As for the alternative solution - it had crossed my mind but I didn’t like it as much
as getting the necessary information from the event arguments.

Thanks.
Abdim


#7

The next release is just in 2-3 days.


#8

Aspose.Word 1.4 is out that supports TableName property.


#9

Thank you - it will be very helpful to us (and hopefully to others too).


#10

Hi Roman,

This doesn’t seem to work … TableName is blank string. I hoped that TableName
would be DataTable.TableName.

I am using either Execute or ExecuteWithRegions method or both. In this particular
case Execute was used.

v1.4.1.0

Regards,
Abdim


#11

Hi Abdim,

In order for MergeFieldEvent.TableName to be returned to you, you must make sure your DataTable.TableName is not blank string before calling MailMerge.Execute. It is just passed back to you and if it was a blank string you will get a blank string.

What overload of Execute or ExecuteWithRegions do you use?


#12

Hi Roman,

I am using Execute and am passing a DataTable (table is part of DataSet) to
MailMerge. All tables have names in the dataset as I select them out by name
(as I pass each individual table to Execute or ExecuteWithRegions). This particular
template doesn’t have any regions so ExecuteWithRegions sometimes gets called in vain
(which I am okay with).

Any thoughts?

Thnx!
Abdim