Free Support Forum - aspose.com

Can you update merge format for fields in existing templates?

We are upgrading from 11.1 to 13.3 (latest version) and in the process have noticed that merge fields that worked in 11.1 no longer work in 13.3 (or even back to 11.2).


We have existing templates (58 of them) where we have the merge fields in the following format :
{ MERGEFIELD fieldName * MERGEFORMAT @ “d” }

In 11.1, this would merge fine and return the day of the month. In 13.3 we get “Error! Picture switch must be first formatting switch”. I’m pretty sure this would be the same result if I merged inside of Word as well.

This can be corrected by updating the merge field to be:
{ MERGEFIELD fieldName @ “d” * MERGEFORMAT }
or
{ MERGEFIELD fieldName @ “d” }

Then 13.3 returns the day of the month as expected. The issue is the volume of templates that worked in 11.1 that are now broke.

Is there a way to update the merge fields format switches without having to manually go through them manually? These templates are client created, so there also is the chance they could upload the files again with the same merge issue. Any help would be appreciated. Thanks.

Hi Christopher,


Thanks for your inquiry. Please note that every new release of Aspose.Words comes up with some new features, enhancements in the existing features and bug fixes.

Aspose.Words tries to mimic the same behavior as MS Word do. If you do the same mail merge via MS Word, you will get the same error. However, you can remove* MERGEFORMAT” from your document by using following code snippet. This will solve your problem. Please let us know if you have any more queries.

<span style=“font-size:
10.0pt;font-family:“Courier New”;color:#2B91AF;mso-no-proof:yes”>Document<span style=“font-size:10.0pt;font-family:“Courier New”;mso-no-proof:yes”> doc = new Document(MyDir

  • “MailMerge-Test2.docx”);<o:p></o:p>

foreach (FieldStart fStart in doc.GetChildNodes(NodeType.FieldStart, true))

{

Node currentNode = fStart.NextSibling;

while (currentNode.NodeType != NodeType.FieldSeparator)

{

if (currentNode.NodeType == NodeType.Run)

{

if((currentNode as Run).Text.Contains(@"\* MERGEFORMAT"))

{

(currentNode as Run).Text = (currentNode as Run).Text.Replace(@"\* MERGEFORMAT", "");

}

}

currentNode = currentNode.NextSibling;

}

}

doc.MailMerge.Execute(

new string[] { "fieldName" },

new object[] { DateTime.Now.Date});

doc.Save(MyDir + @"out.docx");


That worked out great, just had to handle the case where currentNode was null in the while loop. I just moved the mergeformat to the end instead of just removing it. Thanks!

Hi Christopher,


Thanks for your feedback. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.