获取所有超链接的列表?

获取所有超链接的列表?

是否有某种方法可以获取 Microsoft Office 2010 文档中所有超链接的列表?

我正在尝试检查大量的大型文档(Word、Excel 和 PowerPoint 文档的大杂烩)中是否存在断开的链接,但我不想阅读每个文档的每一行来验证我是否拥有所有链接的列表。

答案1

对于 MS WORD,

 Press Alt + F9 to display the fields

Ctrl + F to open the search box

Search: ^d hyperlink

Check "Highlight all items found ..."

Click on the Find All button

Close the dialog

Ctrl + C to copy everything that is highlighted

Open a new document and paste.

对于 Excel,

Close all workbooks except the one you want to find the links in.
On the Edit menu, click Find.
Click Options.
In the Find what box, enter [.
In the Within box, click Workbook.
In the Look In box, click Formulas.
Click Find All.
In the box at the bottom, look in the Formula column for formulas that contain [.
To select the cell with a link, select the row in the box at the bottom.
Links are also commonly used in names, text boxes, or chart titles.

答案2

要列出 Word 文档中的所有超链接:

Sub CheckLinks()
    Set doc = ActiveDocument
    Dim i
    For i = 1 To doc.Hyperlinks.Count
        Debug.Print doc.Hyperlinks(i).Address & " " & doc.Hyperlinks(i).SubAddress
    Next
End Sub

答案3

我发现@user228546 的答案确实很有帮助,因为我无法让我的 Microsoft Word (2013) 版本显示已接受答案中的选项。但是,它有点简短,并且需要对 Visual Basic for Applications (虚拟专用网络) 以使一切正常运行。

这是一个稍微修改过的答案,可以帮助一些不太了解 VBA 的人。

您需要使用Alt+进入 VBA 编辑器F11。使用->顶部的“插入”“模块”,这将打开一个编辑器窗口。


在新文档中获取链接地址

我实际上要将提取的超链接保存到一个新文档中,然后保存它。

在编辑器窗口中输入(或复制/粘贴)以下内容。

Sub GetLinksInNewDoc()
'
' Finds all hyperlinks (even with strange formats,
' as long as they're active)
' and displays them in a new document.
'
    ' Declare the types of our variables
    Dim doc As Document
    Dim newDoc As Document
    Dim hlink As Hyperlink

    ' Use the script on the current document
    Set doc = ActiveDocument
    ' Open a new document to put the link addresses into
    Set newDoc = Documents.Add

    ' Loop through all the hyperlinks using the iterable hlink variable
    With doc
        For Each hlink In .Hyperlinks
            ' Switch into the new document
            newDoc.Activate

            ' Put the Hyperlink Address in the new document
            With Selection
                .InsertAfter hlink.Address & " " & hlink.SubAddress
                .InsertAfter vbNewLine
            End With
        Next hlink
    End With

    Set doc = Nothing
    Set newDoc = Nothing
End Sub

确保带有超链接的文档是您突出显示的最后一个 Microsoft Word 文档。保存您的代码。单击绿色箭头运行,或从上方工具栏中选择“运行” ->“运行子程序/用户窗体”,或按F5

请注意,您可能会得到最终将出现在文档中的文本的“灰色幽灵” - 类似于

灰色幽灵示例


获取TXT文件中的链接地址

现在,如果你真的想将 URL 保存到一个TXT文件中,这就是我提出这个问题的原因,你可以使用相同的过程,只是你的代码应该是

Sub GetLinksInTxtFile()
'
' Finds all hyperlinks (even with strange formats,
' as long as they're active)
' and outputs them to a TXT file.
' The TXT file will be written in the same directory
' as the original document
'
    ' Declare the types of our variables
    Dim doc As Document
    Dim hlink As Hyperlink

    ' Use the script on the current document
    Set doc = ActiveDocument

    ' Get a text file ready to which you will write the URLs
    ' Some old-school BASIC
    Open doc.Path & "\the_urls.txt" For Output As #1

    ' Loop through all the hyperlinks using the iterable hlink variable
    With doc
        For Each hlink In .Hyperlinks
            Print #1, hlink.Address & " " & hlink.SubAddress
        Next hlink
    End With

    Close #1
    Set doc = Nothing
End Sub

我希望它有帮助。


来源据我了解,复制到新文档的概要。

另一个相关来源

来源用于写入文本文件(这是我最初寻找的)

答案4

使用 LibreOffice 和导航器窗格。您可以从文档中查看各种内容,包括超链接

相关内容