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

发布于 2024-09-11 10:56:09 字数 79 浏览 2 评论 0原文

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

Is there a macro, VBA code or VBScript that I can write to edit the urls of all the hyperlinks in my Word document? Either Word 97-2003 or docx format.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

陌路终见情 2024-09-18 10:56:09
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

以下是所有超链接方法和属性的链接

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

Here is a link to all the Hyperlink Methods and Properties

瑕疵 2024-09-18 10:56:09

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

This helped me immensely. User had opened Word Docs containing hyperlinks through her mapped drive instead of going in the long way through network. Hundreds of docs will be saved!

I used mid() function:

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
甜尕妞 2024-09-18 10:56:09

感谢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

Thanks to Tester for the solution, used it for a quick replace:

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文