@joe_lopez Thank you for reporting the problem to us, the current version of Aspose.Words breaks hyperlink with nested hyperlink field.
We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.
Issue ID(s): WORDSNET-25136
You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.
As a workaround, you can check whether merge field is nested into a hyperlink field and insert simple text in this case. I have modified your code so it produces the correct output:
Document doc = new Document(@"C:\Temp\in.docx");
// Fix field names (enclose field names with whitespaces into double quotes)
FixFieldNames(doc);
var fieldNames = new string[] { "S innovation plan URL" };
var fieldValues = new string[] { "https://tasb.org" };
doc.MailMerge.FieldMergingCallback = new HandleMailMergeEvents();
doc.MailMerge.Execute(fieldNames, fieldValues);
doc.Save(@"C:\Temp\out.docx");
private static void FixFieldNames(Document doc)
{
List<Field> mergefields = doc.Range.Fields.Where(f => f.Type == FieldType.FieldMergeField).ToList();
DocumentBuilder builder = new DocumentBuilder(doc);
foreach (Field field in mergefields)
{
Regex regex = new Regex(MERGE_FIELD_REGEX);
Match match = regex.Match(field.GetFieldCode());
string resultFieldCode = string.Format("MERGEFIELD \"{0}\" \\* MERGEFORMAT", match.Groups["fieldName"].Value);
// Move document buidler cursor to the mergefield and insert the corrected mergefield.
builder.MoveToField(field, false);
Field newField = builder.InsertField(resultFieldCode);
// remove old mergefield.
field.Remove();
}
}
const string MERGE_FIELD_REGEX = @"MERGEFIELD (?<fieldName>.+) \\\* MERGEFORMAT";
private class HandleMailMergeEvents : IFieldMergingCallback
{
void IFieldMergingCallback.FieldMerging(FieldMergingArgs e)
{
var fillInText = e.FieldValue.ToString();
// Simple check whether the value is URL. More advanced condition can be used instead
if (fillInText.StartsWith("http"))
{
// Check if the mergefield is nested into the hyperlink field.
bool isNestedInHyperlink = false;
Node currentNode = e.Field.Start.PreviousSibling;
while (currentNode != null)
{
if (currentNode.NodeType == NodeType.FieldStart)
{
isNestedInHyperlink = ((FieldStart)currentNode).FieldType == FieldType.FieldHyperlink;
break;
}
currentNode = currentNode.PreviousSibling;
}
if (!isNestedInHyperlink)
{
DocumentBuilder builder = new DocumentBuilder(e.Document);
// Move cursor to the field and insert hyperlink.
builder.MoveToField(e.Field, false);
builder.InsertHyperlink(fillInText, fillInText, false);
e.Text = "";
}
}
}
void IFieldMergingCallback.ImageFieldMerging(ImageFieldMergingArgs args)
{
}
}