Find Current Page Number of a Hyperlink containing a URL RRS feed

  • Question

  • I am creating a tool that will go through a folder and check all of the word documents for URL links. if it finds a link it tests it to ensure that the links are not broken. As it parses through the document I have the tool generate an Excel spreadsheet report listing the Hyperlink information. I have everything working, but I can not seem to figure out how to get the page number the linked item / text is placed on. It is important to add that information so it is easy for the document owner to find the broken link.

    Here are the sample code that I think will help show how I am doing this.

    The Line I am having issue with is the

      .Range("A" & DocRowCount) = objWord.Selection.Information(wdActiveEndPageNumber)

    This line only reports the page 1. this is because the document is opened but the selection never moves as the links are being tested. I am not sure how to do that and I am not sure if that is the correct thing to do

    Can anyone help?

    *****************Code Segment ******************

                'Open Word Doc
                    'On Error GoTo ErrorSkipDocFile
                    ' Set doc = GetObject(oFile)

                Set objWord = CreateObject("Word.Application")
                objWord.Visible = True
                    FilePath = oFile
                    Set objDoc = objWord.Documents.Open(FilePath)
                    DocRowCount = DocRowCount + 2

                'Search For Links
                    Set colHyperlinks = objDoc.Hyperlinks
                    For Each objHyperlink In colHyperlinks
                    'Check Link if it is good
                        Debug.Print objHyperlink.Address
                        Debug.Print objHyperlink.Name
                        Debug.Print objHyperlink.TextToDisplay
                        If objHyperlink.Address <> "" Then
                            'Test URL
                            strResult = GetResult(objHyperlink.Address)
                        'Report Out

                                .Range("A" & DocRowCount) = objWord.Selection.Information(wdActiveEndPageNumber)
                                .Range("A" & DocRowCount).HorizontalAlignment = xlCenter
                                .Range("B" & DocRowCount) = objHyperlink.TextToDisplay
                                .Range("B" & DocRowCount).WrapText = True
                                .Range("B" & DocRowCount).HorizontalAlignment = xlCenter
                                .Range("C" & DocRowCount) = objHyperlink.Address
                                .Range("C" & DocRowCount).WrapText = True
                                .Range("C" & DocRowCount).HorizontalAlignment = xlLeft
                                .Range("D" & DocRowCount) = strResult
                                .Range("D" & DocRowCount).WrapText = True
                                .Range("D" & DocRowCount).HorizontalAlignment = xlLeft

                            End If
                        Next objHyperlink   ' hyperlink

    Friday, April 29, 2016 11:00 PM


All replies

  • Try:

    .Range("A" & DocRowCount) = objHyperlink.Range.Information(wdActiveEndPageNumber)

    Paul Edstein
    [MS MVP - Word]

    Saturday, April 30, 2016 4:28 AM
  • Hi Smithmi8,

    I test your code and find that suggestion given by the macropod

     able to solve your issue.

    please kindly check the suggestion of macropod.




    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.

    Monday, May 2, 2016 1:49 AM
  • Thank you so much. it seems so simple now looking at the correct code. I have not spent much time working with word. most PowerPoint and Excel.  Once again, Thank You.
    Monday, May 2, 2016 12:21 PM