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

# Calculating sum of mergefields

Hello
I want to do something very simple
Put two merge fields in my doc file and get result of doc file with sum of those two fields
so I put in my doc file"
{ =SUM(«X»,«Y») }
If x=2 and y=3 I expected to get 5
{ =SUM(3,2) }I am using Aspose 6What am I doing wrongThx
Hi Yosi,

First i will suggest you to always use latest version of Aspose.Words.

{ =SUM({ MERGEFIELD val1}, { MERGEFIELD val2} ) }

DataSet ds = new DataSet();

// Open template.

Document doc = new Document("template.doc");

doc.MailMerge.ExecuteWithRegions(ds);

doc.Save("out.doc");

Note:
The { } field delimiters can not be typed using the braces characters on the keyboard. They are special word characters that have to be inserted as pair using Ctrl-F9, or Insert => Field

I think I missed the Ctrl-F9 issue.
But I see someting strange
I do {
=
{ MERGEFIELD val1} + {
MERGEFIELD val2}

}
Then I rap it with Ctrl-F9
I save the doc, close and reopen it and the whole line is gone
What am I missing ?

Hi Yosi,

Thanks for your observation. I also see this behavior on my side. This is MS Word default behavior.
Further, i think you don’t need to wrap it with Ctrl+F9.

Hi Yosi,

Please make sure your field looks like the field in the attached template.

Thanks,

Both your example and mine are working.
But I still have a problem.
After opening the result doc file at the place I suppose to see x+y I see nothing.
I have to select all and press F9 (or right click -> update field).
I tried in my code to put doc.UpdateFields but it’s not working.
Any ideas?

Hi
Yosi,

Thanks for your inquiry. I have created and attached a sample input Word document i.e. containing the desired layout here for your reference. In this case, you can observer, how the merge fields can be wrapped inside formula fields. Please use the following code snippet to calculate Sum of two MergeFields:

Document doc = new
Document(@"c:\test\in
• Copy.docx"
);

doc.MailMerge.Execute(
new string[]
{
“X”, “Y”
},
new object[]
{
“3”, “5”
});

doc.UpdateFields();

doc.Save(@“c:\test\out.docx”);

Moreover, please press Alt+F9 for toggling field codes.
I hope, this will help.

Best Regards,

Thx for your reply but this code will not be good for me.
I use like 150 different merge fields and each doc template will contain a different variation of them.
So I cant implement code to specific fields like you wrote
(

new string[] { “X”, “Y” },
new object[] { "3", "5" });
)
I am looking for a solution for putting sum and 2 merge fields (any fields) and get the sum of them automatic in the new document (without having to press F9)
Basically I just need solution in code that will do the F9.
Any ideas?

Hi

Thank you for additional information. Which version of Aspose.Words do you use? Probably, you use old version of Aspose.Words and it does not support updating formula fields. That is why you get wrong result. Please try using the latest version, you can download it from here:

http://www.aspose.com/community/files/51/.net-components/aspose.words-for-.net/category1188.aspx

Best regards,