How to determine which hyperlink point to bookmark and to heading

Hello Support,

I want to determine which hyperlinks points to bookmark and which one to headings in same document.
I could able to find out which ones goes to external resources by using subaddress property.
Workaround is to loop through bookmarks and and if subaddress is not found in bookmark collection then its pointing to heading.
Please suggest if there is any better way to determine bookmark and heading hyperlink.

Thanks in advance.

Regards
Mandar

Hello Support,
I think internal bookmarks starts with “_” underscore so based on that I can identify if its user created or word created and for word created assumption will be for heading.

Is there any other solution?

Thanks in advance.

Regards
Mandar

Hi Mandar,

Thanks for your inquiry. Please use FieldHyperlink.SubAddress property to get or set a location in the file, such as a bookmark, where this hyperlink jumps.

*mandar_limaye:

I think internal bookmarks starts with “_” underscore so based on that I can identify if its user created or word created and for word created assumption will be for heading.*

Yes, your understating is correct. Please let us know if you have any more queries.

Document doc = new Document(MyDir + "in.docx");
foreach (FieldHyperlink link in doc.Range.Fields.Cast<Field>().ToList().Where(f => f.Type == FieldType.FieldHyperlink))
{
    if (link.SubAddress != null)
    {
        // Your code. 
    }
}

Hello Support,

I already mentioned about using subaddress property, but this is helpful to determine if hyperlinks are pointing to external link or intra document. For determining if its heading or user created bookmark it is necessary to use bookmark name signature.
It would have handy if this is part of property similar to subaddress.

Thanks

Regards
Mandar

Hi Mandar,

Thanks for your inquiry. We have logged this feature request as WORDSNET-14097 in our issue tracking system. You will be notified via this forum thread once this feature is available.

We apologize for your inconvenience.

Hi Mandar,

Thanks for your patience. It is to update you that we have closed the issue (WORDSNET-14097) with “Won’t Fix” resolution.

Hyperlinks which refer to bookmark internal are:

Bookmark : refer to existing bookmark
Heading : refer to auto created hidden bookmark at the beginning of heading paragraph.

But the HYPERLINK field can be created manually and any bookmarks can be used. Moreover, MS Word supports HYPERLINK field that refers heading by it text. Please use following code example to determine either the hyperlink points to bookmark or heading. Hope this helps you.

Document document = new Document(MyDir + "in.docx");
BookmarkCollection bookmarks = document.Range.Bookmarks;
List<Paragraph> headings = document.GetChildNodes(NodeType.Paragraph, true)
    .Cast<Paragraph>().Where(p => p.ParagraphFormat.IsHeading).ToList();
foreach (FieldHyperlink field in document.Range.Fields.OfType<FieldHyperlink>())
{
    Console.Write(field.GetFieldCode() + ": ");
    if (string.IsNullOrEmpty(field.SubAddress))
    {
        Console.WriteLine("does not point to bookmark or heading");
        continue;
    }
    Bookmark bookmark = bookmarks[field.SubAddress];
    if (bookmark == null)
    {
        if (headings.Any(p => p.GetText().TrimEnd(ControlChar.ParagraphBreakChar) == field.SubAddress))
        {
            Console.WriteLine("points to heading by it text");
        }
        else
        {
            Console.WriteLine("does not point to anything");
        }
    }
    else
    {
        if (bookmark.BookmarkStart.NextSibling == bookmark.BookmarkEnd &&
        bookmark.BookmarkStart.ParentNode.NodeType == NodeType.Paragraph &&
        ((Paragraph)bookmark.BookmarkStart.ParentNode).ParagraphFormat.IsHeading)
        {
            Console.WriteLine("points to heading by bookmark");
        }
        else
        {
            Console.WriteLine("points to bookmark");
        }
    }
}

The issues you have found earlier (filed as ) have been fixed in this update. This message was posted using BugNotificationTool from Downloads module by MuzammilKhan