2010-07-28 80 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 
相關問題