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.

Wed Nov 20, 2019 4:32 pm

Hello,

I'm want to use merge field in hyperlink but seems there is no easy way to achieve it. What I can do is to insert normal merge field in the template and execute Mail Merge. But at the end can I find the merged value in the whole document and wrap it in hyperlink field? Generally, can I find "www.google.com" somewhere in a document and make it clickable?

Regards,
Velislav

profiler007
 
Posts: 72
Joined: Wed Nov 13, 2019 11:32 am

Thu Nov 21, 2019 8:29 am

Hi,

Thanks for your inquiry.
Yes you could add a field with a hyperlink in the mail merge process. I have attached my input and result files here, please check them. And below is the code for you.

Code: Select all
            Document doc = new Document();
            doc.LoadFromFile(@"……\input.doc");
            string[] fieldname = new string[] { "hyperlink text" };
            string[] value = new string[] { " www.e-iceblue.com" };
            //Add hyperlink in the MergeFieldEventHandler
            doc.MailMerge.MergeField += new MergeFieldEventHandler(MailMerge_MergeField);
            doc.MailMerge.Execute(fieldname, value);
            doc.SaveToFile("result.doc", FileFormat.Doc);
        }

        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;

            //Apply style
            ParagraphStyle style = new ParagraphStyle(mergefield.Document);
            style.Name = "MyStyle";
            style.CharacterFormat.TextColor = Color.Blue;
            style.CharacterFormat.FontSize = 10.0f;
            style.CharacterFormat.UnderlineStyle = UnderlineStyle.Single;
            mergefield.Document.Styles.Add(style);
            mergefield.OwnerParagraph.ApplyStyle(style.Name);
        }

If the code couldn't solve your issue, please offer us your desired result file for further investigation. You could upload it here or send us(support@e-iceblue.com) via email.

Best wishes,
Amber
E-iceblue support team
User avatar

Amber.Gu
 
Posts: 525
Joined: Tue Jun 04, 2019 3:16 am

Thu Nov 21, 2019 4:14 pm

Works fine :D Thanks!

profiler007
 
Posts: 72
Joined: Wed Nov 13, 2019 11:32 am

Fri Nov 22, 2019 1:04 am

Hi,

Thanks for your feedback.
Any question, welcome to contact us. Have a nice day.

Best wishes,
Amber
E-iceblue support team
User avatar

Amber.Gu
 
Posts: 525
Joined: Tue Jun 04, 2019 3:16 am

Return to Spire.Doc