We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Adding Hyperlinks during MailMerge

Hello,

I’m using a MailMerge to create a report with N number of records. Currently, I have some data such as name, description, date. I would like to replace the Name field with a hyperlink. Is there a simple way to do this during the MailMerge?

Or, will I have to put in some dummy text, then search/replace that after the MailMerge executes? If so, is there a way to move the DocumentBuilder to a node without using a ReplaceAction. The following code from another post is somewhat helpful:

private ReplaceAction ReplaceEvaluatorWithoutReplace(object sender, ReplaceEvaluatorArgs e)
{
    builder1.MoveTo(e.MatchNode);
    // The following code line can show you where actually DocumentBuilder is positioned to.
    // builder1.Write("Found it!");
    return ReplaceAction.Skip;
}

But, this requires the use of a static DocumentBuilder, which isn’t threadsafe. I’m looking for a way to move the builder to a location based on some known text. Then, I can simply use builder.InsertField() to create the hyperlink. Of course, I would then have to delete the remaining text that was used as a marker, but that shouldn’t be too difficult.

Thanks,

Balyn

Hi

Thanks for your inquiry. I think that you can use MergeField event to achieve this. See the following code.

public void TestMailMerge_99289()
{
    DataTable table = new DataTable("table");
    table.Columns.Add("Name");
    for (int i = 0; i < 10; i++)
    {
        DataRow row = table.NewRow();
        row[0] = "Aspose";
        table.Rows.Add(row);
    }
    Document doc = new Document(@"282\_99289\_Balyn\in.doc");
    doc.MailMerge.MergeField += new MergeFieldEventHandler(MailMerge\_MergeField\_99289);
    doc.MailMerge.ExecuteWithRegions(table);
    doc.Save(@"282\_99289\_Balyn\out.doc");
}

void MailMerge_MergeField_99289(object sender, MergeFieldEventArgs e)
{
    if (e.FieldName == "Name")
    {
        DocumentBuilder builder = new DocumentBuilder(e.Document);
        builder.MoveToField(e.Field, true);
        builder.Font.Color = Color.Blue;
        builder.Font.Underline = Underline.Single;
        builder.InsertHyperlink(e.FieldValue.ToString(), "http://www.aspose.com", false);
        e.Text = string.Empty;
    }
}

Also see the following link.
https://reference.aspose.com/words/net/aspose.words.mailmerging/ifieldmergingcallback

Best regards.

Thanks. That is exactly what I needed.

Balyn