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

Free Support Forum - aspose.com

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
Instead I get the string
{ =SUM(3,2) }

I am using Aspose 6
What am I doing wrong
Thx
Hi Yosi,

Thanks for your inquiry.
First i will suggest you to always use latest version of Aspose.Words.
http://www.aspose.com/community/files/51/.net-components/aspose.words-for-.net/default.aspx

To calculate two merge fields sum, please follow up this syntax:
{ =SUM({ MERGEFIELD val1}, { MERGEFIELD val2} ) }

DataSet ds = new DataSet();

ds.ReadXml("dataset.xml");

// 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

thx for your reply
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.

Please feel free to ask any further query.

Hi Yosi,


Thanks for your inquiry.

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

If we can help you with anything else, please feel free to ask.

Thanks,

Thx Adam
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,

Thx for your reply
I use version 6
Is that feature supported on version 6?
I cant change it on production now but maybe on the future

Hi Yosi,

Thanks for your inquiry. Starting from the release of Aspose.Words for .NET 7.0, Aspose.Words provides a very serious implementation of the formula engine and support for the = (Formula) Fields . For more details, please visit the following link:

Moreover, I would strongly recommend you please upgrade to Aspose.Words v.10.8.0 as unfortunately, we only provide forum support for the latest released version of Aspose.Words. You can download the latest version of Aspose.Words from here:

If we can help you with anything else, please feel free to ask.