Question

I have some mail merge data from a data source which is sometimes used for display and sometimes used for calculation. How can I get Word to convert the value before a calculation?

For example, the field "Cash" comes from the datasource as 100.00CR (which in this case implies a negative value). I need to display the value as is in one place (with the CR), in another part of the mail merge I need to perform a calculation using this value. The problem is that Word thinks the value is a string because of the CR appended to the end of the value.

I was thinking I could write a Macro to convert the value prior to doing a calculation, eg,

{ =(ConvertToCalculationValue(110.00) *3)}

but this doesn't seem to work... Any ideas how I can do this without adjusting the data in the datasource?

Was it helpful?

Solution

In the situation where you have a number followed by text, you should be able to do this:

{ SET X { MERGEFIELD theamount } }{ =X*3 }

where you write the name of your merge field instead of "theamount"

(As usual, all the {} have to be the special field code braces that you can insert using ctrl-F9 in Windows Word).

If you need to test whether or not "theamount" ends with "CR", you can use

{ IF "{ MERGEFIELD theamount }" = "*CR" "it ends with CR" "it doesn't end with CR" }

You may encounter problems if the text following the amount happens to be the currency symbol set up in your Regional Settings.

BTW, there is no (practical) way to invoke Word VBA macros/functions from within the "Field language". IN general it is best to try to ensure that your data source has your data in the format(s) that the merge needs, but...

  1. If the data source understands SQL (e.g. Access, Excel and text data sources connected via OLEDB or ODBC understand Jet SQL) you can use the SQLStatement parameter of Word VBA's OpenDataSOurce method to do some data transformation. But beware, you get an absolute maximum of 511 characters of SQL to do that.
  2. If your data source doesn't understand SQL (or only understands the crude dialect that Word implements internally), in some circumstances you can use a DATABASE field connected to a Jet .mdb to apply built-in Access VBA functions to your data
  3. The "approved" method is to use Word's MailMerge events to modify the data, typically inserting the data by setting Document Variables and using { DOCVARIABLE } fields.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top