代码之家  ›  专栏  ›  技术社区  ›  jinsungy

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

  •  11
  • jinsungy  · 技术社区  · 14 年前

    是否有宏、vba代码或vbscript可用于编辑Word文档中所有超链接的URL?Word 97-2003或Docx格式。

    3 回复  |  直到 6 年前
        1
  •  12
  •   Tester101    12 年前
    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

        2
  •  0
  •   Lee Taylor Dejan.S    11 年前

    这对我帮助很大。用户通过映射的驱动器打开了包含超链接的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
    
        3
  •  0
  •   Chema    6 年前

    由于测试人员提供了解决方案,因此使用它进行快速更换:

    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