如何以编程方式编辑Word文档中的所有超链接?

13

有没有一种宏、VBA代码或VBScript可以编写,来编辑Word文档中所有超链接的URL?支持Word 97-2003或docx格式。


您想进行哪种类型的编辑?您是想循环遍历每个超链接,还是对每个超链接进行相同的更改? - NakedBrunch
基本上,我想对每个超链接进行替换。文件服务器名称已更改。 - jinsungy
3个回答

14
Dim doc As Document
Dim link, i
'Loop through all open documents.
For Each doc In Application.Documents
    'Loop through all hyperlinks.
    For i = 1 To doc.Hyperlinks.Count
        'If the hyperlink matches.
        If LCase(doc.Hyperlinks(i).Address) = "http://www.yahoo.com/" Then
            'Change the links address.
            doc.Hyperlinks(i).Address = "http://www.google.com/"
            'Change the links display text if desired.
            doc.Hyperlinks(i).TextToDisplay = "Changed to Google"
        End If
    Next
Next

这里有一个链接,它包含了所有的超链接方法和属性


2
这不适用于带有超链接的图像 =/ 你知道如何获取那些吗? - Roy Calderon

0

这对我帮助很大。用户通过映射驱动器打开了包含超链接的Word文档,而不是通过网络的长路径。数百个文档将被保存!

我使用了mid()函数:

Sub FixMyHyperlink()

    Dim doc As Document
    Dim link, i

    'Loop through all open documents.
    For Each doc In Application.Documents
        'Loop through all hyperlinks.
        For i = 1 To doc.Hyperlinks.Count
            'If the hyperlink matches.
            If LCase(doc.Hyperlinks(i).Address) Like "*partOfHyperlinkHere*" Then
                'Change the links address. Used wildcards (*) on either side.
                doc.Hyperlinks(i).Address = Mid(doc.Hyperlinks(i).Address, 70,20)        '
                'Change the links display text if desired.
                'doc.Hyperlinks(i).TextToDisplay = "Oatmeal Chocolate Chip Cookies"
            End If
        Next
    Next
End Sub

0

感谢 Tester 提供的解决方案,我用它快速进行了替换:

Sub ReplaceLinks()

Dim doc As Document
Dim link, i

'Loop through all open documents.
For Each doc In Application.Documents
    'Loop through all hyperlinks.
    For i = 1 To doc.Hyperlinks.Count

        'Update old bookmarks to https
        doc.Hyperlinks(i).Address = Replace(doc.Hyperlinks(i).Address, "gopher:", "https://")

   Next
Next
End Sub

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接