有没有一种宏、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
这里有一个链接,它包含了所有的超链接方法和属性
。这对我帮助很大。用户通过映射驱动器打开了包含超链接的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
感谢 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