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

时间:2010-07-28 16:47:43

标签: vbscript ms-word word-vba

我是否可以编写宏,VBA代码或VBScript来编辑Word文档中所有超链接的URL? Word 97-2003或docx格式。

3 个答案:

答案 0 :(得分:13)

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

这是指向所有Hyperlink Methods and Properties

的链接

答案 1 :(得分:0)

这对我帮助很大。用户通过其映射驱动器打开了包含超链接的Word Docs,而不是通过网络进行长途操作。数以百计的文档将被保存!

我使用了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

答案 2 :(得分: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