2010-07-28 83 views
9

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

+0

你想要什么样的编辑做什么呢?你想循环访问每个超链接还是对每个链接进行相同的更改? – 2010-07-28 16:57:28

+0

基本上我想在每个超链接上进行替换。文件服务器名称已更改。 – jinsungy 2010-07-28 17:00:43

回答

10
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

+0

完美工作。谢谢。 – jinsungy 2010-07-29 14:29:29

+0

帮助我,谢谢! – 2012-03-30 05:25:46

+1

这不适用于超链接图像= /你知道如何得到这些图像吗? – 2016-04-17 01:29:46

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 
相关问题