Change merge field definition in code

I am trying to get around the word 255 character limit in merge fields and do not know how to change it in the code. We are storing the object path in the merge field, ie. TableStart:Policy.Claims[] to iterate through a policies claims. Only problem is when we have a really long one, with conditions, we hit the 255 character limit. I would like to change the definition before the merge starts. I will have a database table that has 2 columns, alias and definition. Alias is the PK, and definition will contain the text I want to change the merge field to be. For example:


tbs:aliasKey

would be resolved to
TableStart:Policy.Claims[]

The code I am trying is:
// Now replace the guts of the merge field
NodeCollection starts = m_wordDoc.GetChildNodes(NodeType.FieldStart, true);

for (int j = 0; j < starts.Count; j++)
{
FieldStart fStart = (FieldStart)starts[j];
if (fStart.FieldType == FieldType.FieldMergeField)
{
m_wordDocBuilder.MoveToField(fStart.GetField(), false);
FieldStart newStart = (FieldStart)m_wordDocBuilder.CurrentNode;

 <span style="color:#4ec9b0;">String</span>[] fieldcodes <span style="color:#b4b4b4;">=</span> fStart<span style="color:#b4b4b4;">.</span>GetField()<span style="color:#b4b4b4;">.</span>GetFieldCode()<span style="color:#b4b4b4;">.</span>Trim()<span style="color:#b4b4b4;">.</span>Split(<span style="color:#569cd6;">new</span> <span style="color:#4ec9b0;">Char</span>[] { <span style="color:#d69d85;">' '</span> });
 <span style="color:#569cd6;">if</span> (fieldcodes[<span style="color:#b5cea8;">2</span>] <span style="color:#b4b4b4;">!=</span> <span style="color:#d69d85;">""</span> <span style="color:#b4b4b4;">&&</span> fieldcodes[<span style="color:#b5cea8;">2</span>]<span style="color:#b4b4b4;">.</span>IsEqualTo(originalField))
 {
    fStart<span style="color:#b4b4b4;">.</span>GetField()<span style="color:#b4b4b4;">.</span>Result <span style="color:#b4b4b4;">=</span> <span style="color:#d69d85;">@"MERGEFIELD "</span> <span style="color:#b4b4b4;">+</span> field <span style="color:#b4b4b4;">+</span> <span style="color:#d69d85;">@" \* MERGEFORMAT"</span>;
 }

}
}


The output after the merge is still the same as in the input - I’m guessing the GetField().Result is not the way to do it. Any suggestions are appreciated!

Thank you in advance.

Hi Sherri,

Thanks for your inquiry. It would be great if you please share following detail for investigation purposes.

  • Please attach your input Word document.
  • Please

    create a standalone/runnable simple application (for example a Console
    Application Project
    ) that demonstrates the code (Aspose.Words code) you used to generate
    your output document

  • Please attach the output Word file that shows the undesired behavior.
  • Please
    attach your target Word document showing the desired behavior. You can
    use Microsoft Word to create your target Word document. I will
    investigate as to how you are expecting your final document be generated
    like.

As soon as you get these pieces of information to
us we’ll start our investigation into your issue.

Regarding setting field’s result, please do not specify MERGEFIELD and MERGEFORMAT in Field’s result. See the following line of code.


fStart.GetField().Result
= “Field Value”;

Please see attached solution for an example. There are comments in the code that will point out what I am after - please let me know if you have any questions, and thank you for your help.

Hi Sherri,

Thanks for sharing the detail.

sgiltner@gotropics.com:

// And at this point doing a m_wordDoc.MailMerge.GetFieldNames() returns the original values,
// not the updated values. How do I get the updated vales to persist?

MailMerge.GetFieldNames method returns a collection of mail merge field names available in the document. In your code, you are setting the result of mail merge fields. You need to rename the mail merge fields. I have modified your code. Please check the following highlighted code snippet.


m_wordDoc = new Document(“__test.docx”);

m_wordDocBuilder = new DocumentBuilder(m_wordDoc);

// There are 3 merge fields

// 0 : dbs:test

// 1 : PolicyUnitId

// 2 : dbe:test

string[] fieldNames = m_wordDoc.MailMerge.GetFieldNames();

for (int i = 0; i < fieldNames.Length; i++)

{

string field = fieldNames[i];

NodeCollection starts = m_wordDoc.GetChildNodes(NodeType.FieldStart, true);

for (int j = 0; j < starts.Count; j++)

{

FieldStart fStart = (FieldStart)starts[j];

if (fStart.FieldType == FieldType.FieldMergeField)

{

m_wordDocBuilder.MoveToField(fStart.GetField(), false);

FieldStart newStart = (FieldStart)m_wordDocBuilder.CurrentNode;

String[] fieldcodes = fStart.GetField().GetFieldCode().Trim().Split(new Char[] { ' ' });

if (fieldcodes[2] != "" && m_replacements.ContainsKey(field) && fieldcodes[2].Equals(field, StringComparison.CurrentCultureIgnoreCase))

{

// This should update fields 0 to TableStart:Policy.PolicyUnits[],

// and 2 to TableEnd:Policy.PolicyUnits[]

m_wordDocBuilder.MoveToMergeField(fieldcodes[2]);

m_wordDocBuilder.InsertField(@"MERGEFIELD " + m_replacements[field] + @" \* MERGEFORMAT");

//fStart.GetField().Result = m_replacements[field];

break;

}

}

}

}

m_wordDoc.Save("Out.docx");


Please note that a field in a Word document is a complex structure consisting of multiple nodes that include field start, field code, field separator, field result and field end. Fields can be nested, contain rich content and span multiple paragraphs or sections in a document. The Field class is a "facade" object that provides properties and methods that allow to work with a field as a single object.

The Start, Separator and End properties point to the field start, separator and end nodes of the field respectively.

The content between the field start and separator is the field code. The content between the field separator and field end is the field result. The field code typically consists of one or more Run objects that specify instructions. The processing application is expected to execute the field code to calculate the field result.

Works perfectly! Thank you very much for your help.

Do you know if the InsertField has the 255 character limitation?

Hi Sherri,

Thanks for your inquiry. Please note that Aspose.Words mimics the same behavior as MS Word does. You can insert mail merge fields with name containing more then 255 characters using Aspose.Words.

Great - thank you for all your help!

Hi Sherri,

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