【发布时间】:2020-07-04 06:35:55
【问题描述】:
是否可以编写宏、VBA 代码或 VBScript 来编辑 Word 文档中所有超链接的 url? Word 97-2003 或 docx 格式。
【问题讨论】:
-
您想要进行什么样的编辑?您要循环浏览每个超链接还是对每个超链接进行相同的更改?
-
基本上我想对每个超链接进行替换。文件服务器名称已更改。
是否可以编写宏、VBA 代码或 VBScript 来编辑 Word 文档中所有超链接的 url? Word 97-2003 或 docx 格式。
【问题讨论】:
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的链接
【讨论】:
这对我帮助很大。用户通过她的映射驱动器打开了包含超链接的 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
【讨论】:
感谢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
【讨论】: