Free Support Forum - aspose.com

Hyperlinks using a merge field on the word document?

Hi

I’m having an issue trying to do the following with an application I’ve created using c#.Net 2010:

1. I’ve got a document with a number of mergefields added to it.
2. One of the fields is actually a website address (FieldName called Website), however, I have currently only added this as a normal mergefield which does not automatically convert into a hyperlink when I do a Document.MailMerge.Execute(DataRow);

What I’d like to find out is if there is a way to actually setup the mergefield in the word document to be a hyperlink.
ie. display www.google.co.za as a hyperlink when the report is generated.

And following on from that, is there a way to change what the display text is? ie. I’d like to set this to another mergefield if possible (FieldName called Name).

I’ve added a template eg. for you to see how I have added the mergefields and also what I’ve tried.

PS. The field may not always be filled in, in which case it needs to be suppressed (which my other mailmerge fields do automatically), and 2ndly, I’d need to be able to use both methods of display, so show the display text as the website address or the actual Name field. (I’ve thought of using the FieldMergeCallBack functions, but that would limit it only using one method).

Thank you.

Kind regards.
Kamla

Hi Kamla,


Thanks for your inquiry. To be able to replace the MergeField text with a Hyperlink, please use the following code snippet:

Document doc = new
Document(@“C:\Temp\Aspose+template+eg.docx”);

doc.MailMerge.FieldMergingCallback = new HandleMergeFields();

doc.MailMerge.Execute(new string[] { "Website" }, new object[] { "http://www.google.co.za" });

doc.Save(@"C:\Temp\out.docx");


private class HandleMergeFields : IFieldMergingCallback

{

void IFieldMergingCallback.FieldMerging(FieldMergingArgs e)

{

if (mBuilder == null)

mBuilder = new DocumentBuilder(e.Document);

if (e.FieldName.Equals("Website"))

{

mBuilder.MoveToMergeField(e.FieldName);

mBuilder.Font.StyleIdentifier= StyleIdentifier.Hyperlink;

mBuilder.InsertHyperlink("Google", e.FieldValue.ToString(), false);

}

}

public void ImageFieldMerging(ImageFieldMergingArgs args)

{

}

private DocumentBuilder mBuilder;

}


I hope, this helps.

Best Regards,

Hi Awais

Yes, I did think that was the only way to do it, but that means I’ll have to limit it to only one display method, which in this case would be: mBuilder.InsertHyperlink(e.FieldValue.ToString(),
e.FieldValue.ToString(), false);



What I’d like to know is if the below is also possible somehow:


doc.MailMerge.Execute(new
string[] { “Website”, “WebsiteName”
}, new object[]
{ http://www.google.co.za”, “Google” });


I.e. to use WebsiteName as a mergefield somehow?

e.g. if I have 10 rows with different Website addresses, and corresponding WebsiteNames. With your method they will all say Google with different hyperlinks attached in the document outputted. Is there someway to get this to be outputted in the correct format format?
Please keep in mind I would need both methods to work correctly.

Thanks.

Kamla

Hi Kamla,


Thanks for your inquiry. One way around would be to just concatenate the two field’s data (in case one field in your data source represents the ‘display name’ and one represents the actual ‘url’) into a single temporary field such that both field’s data is separated by some delimiter for example a comma. Please see the following changes:

Document doc = new
Document(@“C:\Temp\Aspose+template+eg.docx”);

doc.MailMerge.FieldMergingCallback = new HandleMergeFields();

doc.MailMerge.Execute(new string[] { "Website" }, new object[] { "Google,http://www.google.co.za" });

doc.Save(@"C:\Temp\out.docx");



private class HandleMergeFields : IFieldMergingCallback

{

void IFieldMergingCallback.FieldMerging(FieldMergingArgs e)

{

if (mBuilder == null)

mBuilder = new DocumentBuilder(e.Document);

if (e.FieldName.Equals("Website"))

{

string[] splits = e.FieldValue.ToString().Split(new char[] { ',' });

string displayName = splits[0];

string url = splits[1];

mBuilder.MoveToMergeField(e.FieldName);

mBuilder.Font.StyleIdentifier = StyleIdentifier.Hyperlink;

if (string.IsNullOrEmpty(displayName))

mBuilder.InsertHyperlink(url, url, false);

else

mBuilder.InsertHyperlink(displayName, url, false);

}

}

public void ImageFieldMerging(ImageFieldMergingArgs args)

{

}

private DocumentBuilder mBuilder;

}


I hope, this helps.

Best Regards,