Insert link to a network folder or file

I am struggling on this for days! I am trying to insert/modify a link in my template document to insert links to network folders and network files.
Everything I try, it converts the network location to a web link and it adds "https://localhost:44344/" in front of my folder location
I tried this: https://help.syncfusion.com/file-formats/docio/working-with-word-document
Also, found this sample: https://www.syncfusion.com/forums/135566/link-in-mailmergefield
I even tried inserting html with paragraph.OwnerTextBody.InsertXHTML("<a rel='nofollow' href=\"" + args.FieldValue + "\" >test</a>", paraIndex, paraItemIndex);
and it still didn't work.
I'm out of ideas!

Here is the code:

public void MergeFieldEvent(object sender, MergeFieldEventArgs args)
        {
            if (args.FieldName.EndsWith("_Link"))
            {
                InsertHyperlink_MergeField(sender, args);

                if (true == false)
                {
                    WParagraph paragraph = args.CurrentMergeField.OwnerParagraph;
                    int paraIndex = paragraph.OwnerTextBody.ChildEntities.IndexOf(paragraph);
                    int paraItemIndex = paragraph.ChildEntities.IndexOf(args.CurrentMergeField);

                    string html_string = "<a rel='nofollow' href=\"" + args.FieldValue + "\" >test</a>";

                    paragraph.OwnerTextBody.InsertXHTML(html_string, paraIndex, paraItemIndex);
                }
            }
        }

        private static void InsertHyperlink_MergeField(object sender, MergeFieldEventArgs args)
        {
            // Applies the hyperlink style to the merge field.
            IEntity entity = args.CurrentMergeField.NextSibling;
            WFieldMark separator = null;

            //Iterates to the separator following the merge field.
            while (entity != null && !(entity.EntityType == EntityType.FieldMark))
            {
                //Get next sibling item.
                entity = (entity as ParagraphItem).NextSibling;
            }

            separator = entity as WFieldMark;
            WFieldMark nextSibling = separator.NextSibling as WFieldMark;

            //If need to preserve formats,
            //then sets format for resultant text, i.e., sibling item of separator.
            if ((args.CurrentMergeField.Document.Settings.MaintainFormattingOnFieldUpdate
                || args.CurrentMergeField.FieldCode.Contains("\\* MERGEFORMAT"))
                && (separator != null && separator.Type == FieldMarkType.FieldSeparator)
                && !(nextSibling != null && nextSibling.Type == FieldMarkType.FieldEnd))
                (separator.NextSibling as ParagraphItem).ApplyStyle("Hyperlink");
            //If not need to preserve formats while updating,
            //then set formats for merge field directly.
            else
                (args.CurrentMergeField as WMergeField).ApplyStyle("Hyperlink");

            // Inserts hyperlink during Mail merge.
            ParagraphItemCollection items = null;
            int index = 0;

            if (args.CurrentMergeField.Owner is WParagraph)
            {
                WParagraph paragraph = args.CurrentMergeField.OwnerParagraph;
                items = paragraph.Items;
                index = items.IndexOf(args.CurrentMergeField);
            }
            else if (args.CurrentMergeField.Owner is InlineContentControl)
            {
                InlineContentControl inlineContentControl = args.CurrentMergeField.Owner as InlineContentControl;
                items = inlineContentControl.ParagraphItems;
                index = items.IndexOf(args.CurrentMergeField);
            }

            if (items != null)
            {
                //foreach(Entity en in items)
                //{
                //    if(en.EntityType == EntityType.Field)
                //    {
                //        if ((en as WField).FieldType == FieldType.FieldHyperlink)
                //        {
                //            Hyperlink hyperlink = new Hyperlink(en as WField);

                //            string FilePath = @"\\xxxxxxxxxxxx\yyyyyyyyyyyyyy\zzzzzzzzzzzz";

                //            //hyperlink.Uri = (string)args.FieldValue;
                //            hyperlink.FilePath = FilePath.Replace("\\", "\\\\");

                //            //hyperlink.Uri = (string)args.FieldValue;
                //            hyperlink.TextToDisplay = "testing new name";

                //            break;
                //        }
                //    }
                //}

                
                // Creates new hyperlink field.
                WField hyperlinkField = new WField(args.Document);
                hyperlinkField.FieldType = FieldType.FieldHyperlink;

                Hyperlink hyperlink = new Hyperlink(hyperlinkField);
                hyperlink.Type = HyperlinkType.FileLink;
                hyperlink.FilePath = @"\\xxxxxxxxxxx\yyyyyyyyyyyyyy\zzzzzzzzzzzz";
                hyperlink.TextToDisplay = "test text display";

                items.Insert(index, hyperlinkField);
                index++;

                WTextRange textRange = new WTextRange(args.Document);
                //textRange.Text = " HYPERLINK \"" + ((string)args.FieldValue).Replace("\\", "\\\\") + "\" ";

                textRange.Text = " " + args.FieldValue;
                items.Insert(index, textRange);
                index++;

                // Creates field separator mark for hyperlink field.
                WFieldMark fieldSeparator = args.Document.CreateParagraphItem(ParagraphItemType.FieldMark) as WFieldMark;
                fieldSeparator.Type = FieldMarkType.FieldSeparator;
                items.Insert(index, fieldSeparator);
                index += 5;

                // Creates field end mark for hyperlink field.
                WFieldMark fieldEnd = args.Document.CreateParagraphItem(ParagraphItemType.FieldMark) as WFieldMark;
                fieldEnd.Type = FieldMarkType.FieldEnd;
                items.Insert(index, fieldEnd);
                
            }
        }

8 Replies 1 reply marked as answer

HC Hemalatha Chiranjeevulu Syncfusion Team May 24, 2021 12:15 PM UTC

Hi Michael,

Thank you for contacting Syncfusion support.

From the details, we found that your requirement is to insert the folder or file as link in the Word document. To achieve this requirement, we suggest you to use code example from the below link
https://help.syncfusion.com/file-formats/docio/working-with-paragraph#working-with-hyperlinks

Regarding Also, found this sample:
The sample which you tried is an example to append the email as hyperlink in the Word document using mail merge. So, it didn’t help you to achieve your requirement.

We have prepared the sample application to achieve your requirement. Please use the below sample application at your end to insert the file as link in the Word document.
https://www.syncfusion.com/downloads/support/forum/165684/ze/FileHyperlink-902490003

If we misunderstood any of your requirement, please share the input Word document and expected output Word document. Thereby, we will check on your scenario and will try any other feasibility to achieve your requirement.

Please let us know if you have any other questions.

Regards,
Hemalatha C



HC Hemalatha Chiranjeevulu Syncfusion Team May 24, 2021 04:53 PM UTC

Hi Michael,

Please ignore the previous update.

From the details, we found that your requirement is to insert the folder or file as link in the Word document. To achieve this requirement, we suggest you to use code example from the below link
https://help.syncfusion.com/file-formats/docio/working-with-paragraph#working-with-hyperlinks

Regarding Also, found this sample:
The sample which you tried is an example to append the email as hyperlink in the Word document using mail merge. So, it didn’t help you to achieve your requirement.

We have prepared the sample application to insert the folder or file as link during mail merge and it can be downloaded from the below link
https://www.syncfusion.com/downloads/support/forum/165684/ze/FileHyperlink-1355886187

In the above sample, we have done the following things
 1. Loads the template Word document
.2. Executes the mail merge
.3. Insert the file hyperlink during mail merge execution using MailMerge Event
.4. Saves the Word document

Please let us know if you have any other questions.

Regards,
Hemalatha C



MI Michael May 25, 2021 03:37 PM UTC

Thanks for the response, but it's still not working for me

It's converting the network location from this:
\\network_drive1\folder1\folder2\folder3\folder4

To this:
https://localhost:44344/network_drive1/folder1/folder2/folder3/folder4


LB Lokesh Baskar Syncfusion Team May 26, 2021 04:25 PM UTC

Hi Michael,

Thank you for your update. 

Could you please let us know whether the network folders and network files that you mentioned are the generated file location or it is from the weblink location. And share the input document and expected output document/screenshot which contains hyperlink file location.  Could you please modify the below sample given at our end. Thereby, we will check on your scenario and share the details earlier. 
https://www.syncfusion.com/downloads/support/forum/165684/ze/DocIOSample-1232569916.zip 

If you face any difficulties to provide modified sample means, then we would like to set up a web meeting with you to check at your end and provide you more details on this problem. Could you please let me know of your availability for this? Our team will make every effort to have this scheduled on a date and time per your convenience.

Please let us know if you have any other questions.

Regards 
Lokesh B 



MI Michael May 27, 2021 03:26 PM UTC

I am very puzzled by your answer, I'm starting to think you never seen a network folder before.
They are also called UNC folders (Universal Naming Convention)

The puzzling part is that you took my sample folder name:\\network_drive1\folder1\folder2\folder3\folder4
And you turned it into this:
mailMergeData.Add("FileHyperlink", @ResolveApplicationDataPath("/") + "/network_drive1/folder1/folder2/folder3/folder4");

It is not a relative path, it is a fixed path, it's a complete path.

A network folder starts with two \\ and the folders are separated by one \ (those are backslash characters, NOT forward slash)

I can add a network folder in word, and it does not modify it to a web link:


When I hover the mouse over it, it shows correctly in word:


This is what I want to achieve using Syncfusion!


LB Lokesh Baskar Syncfusion Team May 28, 2021 09:03 AM UTC

https://www.syncfusion.com/downloads/support/forum/165684/ze/FileHyperlinkSample-811080617.zip

Output Document:
https://www.syncfusion.com/downloads/support/forum/165684/ze/Output1213743659.zip

In the above sample, we have done the following things
 1. Loads the template Word document
.2. Executes the mail merge
.3. Insert the file hyperlink by append the screen tip switch into field code during mail merge execution using 
MailMerge Event
.4. Saves the Word document

Please let us know if you have any other questions.

Regards, 
Lokesh B


Marked as answer

MI Michael May 28, 2021 03:25 PM UTC

YES!! it's working!

Thank You!


LB Lokesh Baskar Syncfusion Team May 28, 2021 03:53 PM UTC

Hi Michael,

Thank you for your update.

We are glad to know that your problem has been fixed.

Please let us know if you have any other questions and we will be happy to assist you as always.

Regards,
Lokesh B


Loader.
Up arrow icon