I am trying to write a VBA macro for Microsoft Word that will go through the hyperlinks and remove those that contain a particular string.
This is what I initially came up with:
Option Explicit
Sub RemoveLinks()
Dim Position As Integer
Dim Hyperlink As String
Dim i As Integer
Dim NumLinks As Integer
Const ToFind As String = "ENREF"
NumLinks = ThisDocument.Hyperlinks.Count
For i = 1 To NumLinks
Hyperlink = ThisDocument.Hyperlinks(i).SubAddress
Position = InStr(1, Hyperlink, ToFind)
If Position > 0 Then
ThisDocument.Hyperlinks(i).Delete
NumLinks = ThisDocument.Hyperlinks.Count
i = 1
End If
Next i
End Sub
After some of the suggestions here, I also found out that the following code also works:
'VBA macro for Microsoft Word to remove the hyperlink (not the text) when the hyperlink contains a particular string
'https://gist.github.com/04728af4c913f22bd35e65df3dddd43b.git
'Author: ARGYROU MINAS
'SPDX-FileCopyrightText: Copyright 2023-2026 ARGYROU MINAS . Licensed under MIT License.
'SPDX-License-Identifier: MIT
'
'Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
'
'The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
'
'THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
'
'
Option Explicit
Sub RemoveLinksContainingInputString()
Dim Position1 As Integer
Dim Position2 As Integer
Dim Hyperlink1 As String
Dim Hyperlink2 As String
Dim i As Integer
Dim NumLinks As Integer
Dim ToFind As String
ToFind = InputBox("Input the text to match within the hyperlinks.", "Text to match")
NumLinks = ThisDocument.Hyperlinks.Count
For i = NumLinks To 1 Step -1
Hyperlink1 = ThisDocument.Hyperlinks(i).Address
Hyperlink2 = ThisDocument.Hyperlinks(i).SubAddress
Position1 = InStr(1, Hyperlink1, ToFind)
Position2 = InStr(1, Hyperlink2, ToFind)
If (Position1 > 0) Or (Position2 > 0) Then
ThisDocument.Hyperlinks(i).Delete
End If
Next i
End Sub
From what I understand, the reverse loop seems to be faster and more efficient, since it avoids re-initialization. But, does the forward loop have any other drawbacks?
Will this code work properly? It seems to work but, just before it finishes running, I get the error that the requested member does not exist when using the forward loop; why is that?
Lastly, what is the difference between "Address" and "Subaddress"?