I am using the follow function to check if a merge field exists, it’s working for long. However, recently I got an exception for a document, that document contain many chinese character, I don’t know whether it’s the root cause.
I traced in debug mode and found that, the variable got the value " MERGEF", it seems could not get the full node text, the expected string should be " MERGEFIELD field1 \* MERGEFORMAT ".
This occurs randomly, not in every merge field and I could not expect when it would occur.
private bool FieldExists(string name)
{
string s;
string[] s1;
string fieldname;
NodeList fieldStarts = doc.SelectNodes("//FieldStart");
foreach (FieldStart fieldStart in fieldStarts)
{
if (fieldStart.FieldType == FieldType.FieldMergeField)
{
s = fieldStart.NextSibling.GetText();
s1 = s.Split(new char[] { ' ' });
fieldname = s1[3];
if (fieldname == name)
return true;
}
}
return false;
}