Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Tue Mar 20, 2018 1:15 pm

Hi,
it's possible add a field with an hyperlink during the mail merge process?

Thank you.

ryuga79
 
Posts: 4
Joined: Wed Sep 13, 2017 8:19 am

Wed Mar 21, 2018 8:54 am

Hello,

Thanks for your inquiry.
Our Spire.Doc can meet your requirement to add a field with a hyperlink in the mail merge process. Please refer to below sample code.
Code: Select all
Document doc = new Document();
doc.LoadFromFile("Sample.docx");
string[] fieldname = new string[] { "fieldname1", "fieldname2" };
string[] value = new string[] { "value1", "value2" };
//Add hyperlink in the MergeFieldEventHandler
doc.MailMerge.MergeField += new MergeFieldEventHandler(MailMerge_MergeField);
doc.MailMerge.Execute(fieldname, value);           
doc.SaveToFile("Result.docx", FileFormat.Docx);
public static void MailMerge_MergeField(object sender, MergeFieldEventArgs args)
{
     MergeField mergefield = args.CurrentMergeField as MergeField;
     int index = mergefield.OwnerParagraph.ChildObjects.IndexOf(mergefield);
     Field field = new Field(mergefield.Document);
     field.Code = "HYPERLINK \"" + "http://www.e-iceblue.com" + "\"";
     field.Type = FieldType.FieldHyperlink;
     mergefield.OwnerParagraph.ChildObjects.Insert(index,field);

     FieldMark fm = new FieldMark(mergefield.Document, Spire.Doc.Documents.FieldMarkType.FieldSeparator);
     mergefield.OwnerParagraph.ChildObjects.Insert(index+1,fm);

     FieldMark fmend = new FieldMark(mergefield.Document, FieldMarkType.FieldEnd);
     mergefield.OwnerParagraph.ChildObjects.Insert(index+3,fmend);
     field.End = fmend;
}

If I misunderstand your meaning, welcome to get it back and provide more information about your requirement.

Sincerely,
Nina
E-iceblue support team
User avatar

Nina.Tang
 
Posts: 1182
Joined: Tue Sep 27, 2016 1:06 am

Wed Mar 21, 2018 1:08 pm

Thanks for the example but my problem is a bit more complicated.
Link is only one field of a group merge fields.

For example the structure of MailMergeDataTable is:
FirstName, LastName, linkExternalURL

and i need to create a .doc with a table:

FirstName | LastName | Click here to download file (when click open url linkExternalURL)

It's possible?

Thank you.

ryuga79
 
Posts: 4
Joined: Wed Sep 13, 2017 8:19 am

Thu Mar 22, 2018 7:08 am

Dear Cristiano,

Thank your for sharing information.
Yes, it is possible to merge a group using Spire.Doc. I have attached my full code along with used documents for your checking. If the effect is still not what you want, please attach your expected output document here, we will refer to it and provide a solution for you.

Sincerely,
Nina
E-iceblue support team
User avatar

Nina.Tang
 
Posts: 1182
Joined: Tue Sep 27, 2016 1:06 am

Tue Mar 27, 2018 10:12 am

Hello,

Did my solution meet your requirement?
Your feedback will be greatly appreciated.

Sincerely,
Nina
E-iceblue support team
User avatar

Nina.Tang
 
Posts: 1182
Joined: Tue Sep 27, 2016 1:06 am

Return to Spire.Doc