none
Add hyperlink to paragraph inside field in Word 2013 RRS feed

  • Question

  • Hello

    I've been trying to add a hyperlink to a paragraph inside a field but it ends up adding a field hyperlink that is applied to the whole content of the field as opposed to just the paragraph.

    I first write XML to the field and then try processing it so that hyperlinks are created for each paragraph that contains a string "Available at".

    Any help is appreciated.

    I've also asked for help on StackOverflow.

    if (!string.IsNullOrWhiteSpace(bibliography))
                        {
                            const string linkToken = "Available at ";
    
                            field.Result.InsertXML(bibliography);
    
                            foreach (Paragraph paragraph in field.Result.Paragraphs)
                            {   
                                var paragraphText = paragraph.Range.Text;
    
                                var indexOfLink = paragraphText.IndexOf(linkToken, StringComparison.OrdinalIgnoreCase);
    
                                if (indexOfLink >= 0)
                                {
                                    var linkStart = indexOfLink + linkToken.Length;
                                    var linkPart = paragraphText.Substring(linkStart);
    
                                    Uri uriFound;
    
                                    if (Uri.TryCreate(linkPart, UriKind.Absolute, out uriFound))
                                    {
                                        object linkAddress = uriFound.ToString();
    
                                        paragraph.Range.Hyperlinks.Add(paragraph.Range, ref linkAddress);
                                    }
                                }
                            }
                        }

    Thanks

    Tuesday, April 28, 2015 3:06 PM

Answers

All replies

  • Hello

    I've been trying to add a hyperlink to a paragraph inside a field but it ends up adding a field hyperlink that is applied to the whole content of the field as opposed to just the paragraph.

    I first write XML to the field and then try processing it so that hyperlinks are created for each paragraph that contains a string "Available at".

    a hyperlink in a field or a paragraph ? not very clear about this, what I can suggest, you sould  check the paragraph.Range in here

    paragraph.Range.Hyperlinks.Add(paragraph.Range, ref linkAddress);

    Thursday, April 30, 2015 12:23 AM
  • Why not find all your need key words and add hyperlink to it.

    here is an article:  Insert Hyperlink to word in C#,VB.net.

    Thursday, April 30, 2015 3:33 AM