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:
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
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