check all hyperlinks in word document

Discussion in 'Computer Software' started by Riejy, Jun 5, 2017.

Tags:
Rate this thread:
/5,
  1. Ri

    Riejy New Member
    1/6

    Joined:
    Jun 5, 2017
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    1
    Gender:
    Male
    Location:
    UK
    hello

    I just get word document which contains a lot of hyperlinks. But I find that some of the links lead to invalid webpage. Do I have to check them one by one manually? Is there any way can auto check these link for me ? Any ideas? Thanks.
     
  2. Linda

    Linda New Member
    1/6

    Joined:
    Mar 17, 2017
    Messages:
    21
    Likes Received:
    0
    Trophy Points:
    1
    Gender:
    Female
    Location:
    Bangalore
    Dont think as if it were spelling there are options as it hyperlinks, it has to be checked online to see if its correct link.
     
  3. Lo

    Loety New Member
    1/6

    Joined:
    Jun 8, 2017
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    1
    Gender:
    Female
    Location:
    US
    Hi,

    I just find a way using VBA to do that . You need to create a macro and a user form to do that. The macro is as below

    Code:
    Function CheckURL(strURL As String) As Boolean
      Dim objDemand As Object
      Dim varResult As Variant
     
      On Error GoTo ErrorHandler
      Set objDemand = CreateObject("WinHttp.WinHttpRequest.5.1")
     
      With objDemand
        .Open "GET", strURL, False
        .Send
        varResult = .StatusText
      End With
     
      Set objDemand = Nothing
     
      If varResult = "OK" Then
        CheckURL = True
      Else
        CheckURL = False
      End If
     
    ErrorHandler:
    End Function
    
    Sub ReturnURLCheck()
      Dim objLink As Hyperlink
      Dim strLinkText As String
      Dim strLinkAddress As String
      Dim strResult As String
      Dim nInvalidLink As Integer, nTotalLinks As Integer
      Dim objDoc As Document
     
      Application.ScreenUpdating = False
     
      Set objDoc = ActiveDocument
      nTotalLinks = objDoc.Hyperlinks.Count
      nInvalidLink = 0
     
      With objDoc
        For Each objLink In .Hyperlinks
          strLinkText = objLink.Range.Text
          strLinkAddress = objLink.Address
     
          If Not CheckURL(strLinkAddress) Then
            nInvalidLink = nInvalidLink + 1
            strResult = frmCheckURLs.txtShowResult.Text
            frmCheckURLs.txtShowResult.Text = strResult & nInvalidLink & ". Invalid Link Information:" & vbNewLine & _
                                              "Displayed Text: " & strLinkText & vbNewLine & _
                                               "Address: " & strLinkAddress & vbNewLine & vbNewLine
          End If
        Next objLink
     
        frmCheckURLs.txtTotalLinks.Text = nTotalLinks
        frmCheckURLs.txtNumberOfInvalidLinks.Text = nInvalidLink
        frmCheckURLs.Show Modal
     
      End With
      Application.ScreenUpdating = True
    End Sub
    
    Sub HighlightInvalidLinks()
      Dim objLink As Hyperlink
      Dim strLinkAddress As String
      Dim strResult As String
      Dim objDoc As Document
     
      Set objDoc = ActiveDocument
     
      With objDoc
        For Each objLink In .Hyperlinks
          strLinkAddress = objLink.Address
     
          If Not CheckURL(strLinkAddress) Then
            objLink.Range.HighlightColorIndex = wdYellow
          End If
        Next objLink
      End With
    End Sub
    And for the steps to create a user form, please see the article

    https://www.datanumen.com/blogs/batch-validate-hyperlinks-word-document-via-vba/

    hope it helps
     
Rate this thread:
/5,

Currently Active Users Viewing This Thread: 0 (Users: 0, Guests: 0)

Share This Page