vba VBA获取word文档中的所有超链接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19035119/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VBA get all hyperlinks in word document
提问by user1478200
I'm trying to update all hyperlinks in a word document with a macro using Visual Basic. My code updates only the hyperlinks that are not inside a text-box and those inside will not be changed. How can I change all the hyperlinks, including those that are inside any text-boxes as well? I use this code to get my hyperlinks:
我正在尝试使用 Visual Basic 使用宏更新 Word 文档中的所有超链接。我的代码仅更新不在文本框内的超链接,而不会更改其中的超链接。如何更改所有超链接,包括任何文本框中的超链接?我使用此代码来获取我的超链接:
Sub UpdateLinks()
Dim oLink As Hyperlink
links = 0
For Each oLink In ActiveDocument.Hyperlinks
oLink.Range.Bold = 0
oLink.Range.Italic = 0
oLink.Range.Underline = wdUnderlineNone
oLink.Range.Font.Color = wdColorWhite
oLink.Range.Shading.BackgroundPatternColor = wdColorGray375
links = links + 1
Next oLink
End Sub
回答by engineersmnky
This should work for you:
这应该适合你:
Dim links As Integer
Sub UpdateLinks()
links = 0
UpdateDocLinks
UpdateTextBoxLinks
End Sub
Sub UpdateDocLinks()
Dim oLink As Hyperlink
For Each oLink In ActiveDocument.Hyperlinks
links = links + FormatLink(oLink)
Next oLink
End Sub
Sub UpdateTextBoxLinks()
Dim i As Integer
Dim oLink As Hyperlink
For i = 1 To ActiveDocument.Shapes.Count
ActiveDocument.Shapes(i).Select
For Each oLink In Selection.Hyperlinks
links = links + FormatLink(oLink)
Next oLink
Next i
End Sub
Function FormatLink(link As Hyperlink) As Integer
With link.Range
.Bold = 0
.Italic = 0
.Underline = wdUnderlineNone
.Font.Color = wdColorWhite
.Shading.BackgroundPatternColor = wdColorGray375
End With
FormatLink = 1
End Function
DRY versions
干版
Dim links As Integer
Sub UpdateLinks()
links = 0
UpdateDocLinks
UpdateTextBoxLinks
End Sub
Sub UpdateDocLinks()
UpdateLinkSet ActiveDocument.Hyperlinks
End Sub
Sub UpdateTextBoxLinks()
Dim i As Integer
For i = 1 To ActiveDocument.Shapes.Count
ActiveDocument.Shapes(i).Select
UpdateLinkSet Selection.Hyperlinks
Next i
End Sub
Sub UpdateLinkSet(link_set As Variant)
Dim oLink As Hyperlink
For Each oLink In link_set
FormatLink oLink
Next oLink
End Sub
Sub FormatLink(link As Hyperlink)
With link.Range
.Bold = 0
.Italic = 0
.Underline = wdUnderlineNone
.Font.Color = wdColorWhite
.Shading.BackgroundPatternColor = wdColorGray375
End With
links = links + 1
End Sub