none
Is there a way to check the validity of internal hyperlinks inside an active word document? RRS feed

  • Question

  • Hi All,

    I'am trying to build an ms word add-in in which ,

    I wanted to check for a particular scenario,i.e say a user inserts a bookmark and later in the document adds a cross reference to this bookmark.But he then deletes the bookmark and forgets to update his document, now upon pressing ctrl+click (to follow the link) takes me to the start of the document, instead I wanted to know if we can display a specific message to the user telling him to update his document or even  remove the cross reference by any chance .

    Please let me know how I can go about this ,I know how to test for invalid links with respect to toc. But this scenario is confusing.Any help is much much appreciated.Thanks in advance :) 

    Monday, November 10, 2014 7:50 AM

Answers

  • Hi Apoorva,

    For this requirement, you could do validation in the click event of a button or before save document etc…

    You could refer to this code to get the Bookmark of Cross-reference, if the bookmark has been removed it will throw error, you could capture the error to check whether the target bookmark exists.

    Dim doc As Word.Document
    
        Dim fld As Word.Field
    
        Dim rng As Word.Range
    
        Dim str As String
    
        
    
        Set doc = ActiveDocument
    
        For Each fld In doc.Fields
    
            If fld.Type = wdFieldRef Then
    
                str = fld.Code
    
                str = Replace(str, "REF ", "")
    
                str = Replace(str, "\h", "")
    
                str = Trim(str)
    
              ‘need handle the error if the bookmark has been deleted.
    
                Set rng = doc.Bookmarks(str).Range
    
            End If
    
        Next
    

    You also could delete that cross-reference directly by using Field.Result.Delete

    Best 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.

    Tuesday, November 11, 2014 9:10 AM
    Moderator

All replies

  • Hi Apoorva,

    For this requirement, you could do validation in the click event of a button or before save document etc…

    You could refer to this code to get the Bookmark of Cross-reference, if the bookmark has been removed it will throw error, you could capture the error to check whether the target bookmark exists.

    Dim doc As Word.Document
    
        Dim fld As Word.Field
    
        Dim rng As Word.Range
    
        Dim str As String
    
        
    
        Set doc = ActiveDocument
    
        For Each fld In doc.Fields
    
            If fld.Type = wdFieldRef Then
    
                str = fld.Code
    
                str = Replace(str, "REF ", "")
    
                str = Replace(str, "\h", "")
    
                str = Trim(str)
    
              ‘need handle the error if the bookmark has been deleted.
    
                Set rng = doc.Bookmarks(str).Range
    
            End If
    
        Next
    

    You also could delete that cross-reference directly by using Field.Result.Delete

    Best 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.

    Tuesday, November 11, 2014 9:10 AM
    Moderator
  • Thanks a million Starain, works like a charm :) Thank you so much ..had been stuck on the logic for days now :) 
    Wednesday, November 12, 2014 5:44 AM