none
Hyperlink position in document RRS feed

  • Question

  • How can the insertion point be relocated in a Word document to where it finds a hyperlink? E.g. I would like to add some text after each hyperlink in a document. So I traverse the hyperlinks using

    For each hLink in Selection.Hyperlinks

    But when I try to reference hLink.Range it says that property is not defined.

    Any ideas? 

    Tuesday, April 14, 2015 3:13 AM

Answers

  • Thank you all. I got it to work by traversing each paragraph (following jujubeee's and onlie-sandeep's concept), then looking for the hyperlinks in each paragraph. I can then typetext at that point to replace the hyperlink or do whatever else needs to be done.

    Though what Starain chen showed seems more efficient, for some reason, referencing the range of the hyperlink object returns an error about property not supported.

    • Marked as answer by serenejen Wednesday, April 15, 2015 8:00 AM
    Wednesday, April 15, 2015 8:00 AM

All replies

  • Not a  hard thing, refer this article. Download this DOC library then add reference to your project, then use the following code:

    
    
    static void Main(string[] args)
            {
                Document doc = new Document();
                doc.LoadFromFile("Spire.docx");
                List hyperlinks = new List();
                foreach (Section section in doc.Sections)
                {
                    foreach (DocumentObject sec in section.Body.ChildObjects)
                    {
                        if (sec.DocumentObjectType == DocumentObjectType.Paragraph)
                        {
                            foreach (DocumentObject para in (sec as Paragraph).ChildObjects)
                            {
                                if (para.DocumentObjectType == DocumentObjectType.Field)
                                {
                                    Field field = para as Field;
    
                                    if (field.Type == FieldType.FieldHyperlink)
                                    {
                                        hyperlinks.Add(field);
                                    }
                                }
                            }
                        }
                    }
                }

     
    • Edited by jujubeee Tuesday, April 14, 2015 6:39 AM link
    Tuesday, April 14, 2015 6:39 AM
  • Hi serenejen,

    What’s the detail code that you used?

    For your requirement, you could refer to this code below to achieve that:

    Dim h As Hyperlink
    For Each h In Application.Selection.Hyperlinks
     h.Range.Next.InsertBefore "new"
    Next h

    On the other hand, this code is used to add text to each hyperlink of current selection, if you want it applies the whole document, please use ActiveDocument.Hyperlinks.

    Regards

    Starain


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, April 15, 2015 5:46 AM
    Moderator
  • Refer the below given link for solution.

    http://www.e-iceblue.com/Tutorials/Spire.Doc/Spire.Doc-Program-Guide/Hyperlink/NET-Finding-Hyperlinks-in-a-word-document.html

    Regards,

    Wednesday, April 15, 2015 5:50 AM
  • Thank you all. I got it to work by traversing each paragraph (following jujubeee's and onlie-sandeep's concept), then looking for the hyperlinks in each paragraph. I can then typetext at that point to replace the hyperlink or do whatever else needs to be done.

    Though what Starain chen showed seems more efficient, for some reason, referencing the range of the hyperlink object returns an error about property not supported.

    • Marked as answer by serenejen Wednesday, April 15, 2015 8:00 AM
    Wednesday, April 15, 2015 8:00 AM