Tuesday, December 24, 2024

VBA Word - Delete all hyperlinks from document

The following VBA code deletes all hyperlinks from document:
Sub DeleteHyperlinks()
    Dim doc As Document
    Dim hyp As Hyperlink
    Dim i As Integer
    Dim max As Integer
    
    max = ActiveDocument.Hyperlinks.Count
    On Error Resume Next
    For i = 1 To max
        ActiveDocument.Hyperlinks(i).Delete
    Next
End Sub
The following VBA code deletes all hyperlinks from document:
Sub DeleteHyperlinks()
Sub DeleteHyperLinks()
    Dim h As Hyperlink
    For Each h In ActiveDocument.Hyperlinks
        h.Delete
    Next
End Sub
The following VBA code deletes all hyperlinks from document:
Sub DeleteHyperlinks()
    Dim doc As Document
    Dim hyp As Hyperlink
    Dim i As Integer
    Dim max As Integer
    
    max = ActiveDocument.Hyperlinks.Count
    On Error Resume Next
    For i = 1 To max
        ActiveDocument.Hyperlinks(i).Delete
    Next
End Sub
The following VBA code deletes all hyperlinks from document:
Sub DeleteHyperlinkText()
    Dim h As Hyperlink
    Dim rng As Range
    ' Loop backwards to avoid collection shifting
    Dim i As Long
    For i = ActiveDocument.Hyperlinks.Count To 1 Step -1
        Set h = ActiveDocument.Hyperlinks(i)
        Set rng = h.Range
        rng.Delete
    Next i
End Sub
The following VBA code deletes all hyperlinks with its text from document:
Sub DeleteLinesWithHyperlinkContainingQuestion()
    Dim para As Paragraph
    Dim hLink As Hyperlink
    
    ' Loop through paragraphs backward to avoid index issues while deleting
    Dim i As Long
    For i = ActiveDocument.Paragraphs.Count To 1 Step -1
        Set para = ActiveDocument.Paragraphs(i)
        
        ' Check if any hyperlink in this paragraph contains "Question"
        For Each hLink In para.Range.Hyperlinks
            If InStr(1, hLink.TextToDisplay, "Question", vbTextCompare) > 0 Then
                para.Range.Delete
                Exit For ' Exit inner loop after deletion to avoid error
            End If
        Next hLink
    Next i
End Sub
The following VBA code deletes all hyperlinks with its text from document:
Sub DeleteParagraphsWithHyperlinkedQuestionUsingFind()
    Dim rng As Range
    Set rng = ActiveDocument.Content
    
    With rng.Find
        .ClearFormatting
        .Text = "Question[0-9]{1,}"
        .MatchWildcards = True
        .Forward = True
        .Wrap = wdFindStop
        
        Do While .Execute
            ' Check if found text is part of a hyperlink
            If rng.Hyperlinks.Count > 0 Then
                rng.Paragraphs(1).Range.Delete
            Else
                rng.Collapse Direction:=wdCollapseEnd
            End If
        Loop
    End With
End Sub

No comments:

Post a Comment

Hot Topics