Using a Hyperlink with the link and the text presented as Mail Merge Fields

Hello,

I am using the Aspose for a bunch of reports, but only now I came across this obstacle. I want to format an hyperlink in word with the link set to a mail merge and the text presented set to another.

In order to do this I followed the instructions in this site:
https://blog.softartisans.com/2013/12/31/kb-creating-dynamic-links-with-mergefields-in-microsoft-word/
Do you have any advices to do that? Should I follow religiously that information? Because at this point this is not working for me. The principal issue is the link. The text is displayed correctly.

I’d appreciate a lot your help and thank you for your quick answer.

Best regards,
João Carreiro

Hi João,

Thanks for your inquiry. To ensure a timely and accurate response, please attach the following resources here for testing:

  • Your sample input Word document
  • Your expected Word document which shows the correct final output. We will investigate the structure of your expected document as to how you want your final output be generated like. Please create expected document using Microsoft Word. We will then provide you code to achieve the same using Aspose.Words.

As soon as you get these pieces of information ready, we’ll start further investigation into your scenario and provide you more information. Thanks for your cooperation.

PS: To attach these resources, please zip them and Click ‘Reply’ button that will bring you to the ‘reply page’ and there at the bottom you can include any attachments with that post by clicking the ‘Add/Update’ button.

Best regards,

Hello Awais,

First of all, thank you for the quick response.

As you asked I send you attached the input, the docx created to display tables with dynamic names and links in hyperlinks. The issue I’m getting is that the link becomes a static link which doesn’t allow to go to the hyperlink passed in the merge field as expected.

I hope to hear from you soon, with good news.

Any other information that you need to solve this I’m available to give you.

Best regards,
João Carreiro

Hi João,

Thanks for your inquiry. We are working over your query and will get back to you soon.

Best regards,

Hi João,

Thanks for being patient. A simplified workflow to meet this requirement is as follows:

private static DataTable GetDataTable()
{
    DataTable dataTable = new DataTable("FeatureToDoc");
    dataTable.Columns.Add(new DataColumn("RowNumber"));
    dataTable.Columns.Add(new DataColumn("RowNumber_val"));
    DataRow dataRow;
    for (int i = 0; i < 5; i++)
    {
        dataRow = dataTable.NewRow();
        dataRow[0] = "NameOfLink" + i;
        dataRow[1] = "http://www.aspose.com";
        dataTable.Rows.Add(dataRow);
    }
    return dataTable;
}
public class HandleMergeField : IFieldMergingCallback
{
    void IFieldMergingCallback.FieldMerging(FieldMergingArgs e)
    {
        BookmarkStart start = new BookmarkStart(e.Document, "bm_" + e.RecordIndex);
        BookmarkEnd end = new BookmarkEnd(e.Document, "bm_" + e.RecordIndex);
        e.Field.Start.ParentParagraph.PrependChild(end);
        e.Field.Start.ParentParagraph.PrependChild(start);
        e.Text = "";
    }
    void IFieldMergingCallback.ImageFieldMerging(ImageFieldMergingArgs args)
    {
        // Do nothing.
    }
}
Document doc = new Document("Features_input.docx");
DataTable dt = GetDataTable();
doc.MailMerge.FieldMergingCallback = new HandleMergeField();
doc.MailMerge.ExecuteWithRegions(dt);
MemoryStream stream = new MemoryStream();
doc.Save(stream, SaveFormat.Docx);
Document docx = new Document(stream);
DocumentBuilder builder = new DocumentBuilder(docx);
foreach (Bookmark bm in docx.Range.Bookmarks)
{
    if (bm.Name.StartsWith("bm_"))
    {
        int recordIndex = Convert.ToInt16(bm.Name.Split(new char[] { '_' })[1]);
        builder.MoveToBookmark(bm.Name, true, false);
        builder.InsertHyperlink(dt.Rows[recordIndex]["RowNumber"].ToString(), dt.Rows[recordIndex]["RowNumber_Val"].ToString(), false);
    }
}
// remove bm_ type bookmarks if required
docx.Save(MyDir + @"16.6.0.docx");

Hope, this helps.

Best regards,