How do you remove hyperlinks from a Microsoft Word document?

隐身守侯 提交于 2019-12-02 05:29:36
Mike Whyte

This is an old post, so am adding this VBA code in case it is useful to someone.

Hyperlinks (Collections) need to be deleted in reverse order:

Sub RemoveHyperlinksInDoc()
    ' You need to delete collection members starting from the end going backwards
    With ActiveDocument
        For i = .Hyperlinks.Count To 1 Step -1
            .Hyperlinks(i).Delete
        Next
    End With 
End Sub

Sub RemoveHyperlinksInRange()
    ' You need to delete collection members starting from the end going backwards
    With Selection.Range
        For i = .Hyperlinks.Count To 1 Step -1
            .Hyperlinks(i).Delete
        Next
    End With    
End Sub

The line removing the hyperlink is commented out. The following line will remove the first hyperlink within the selected range:

Selection.Range.Hyperlinks(1).Delete

This will also decrement Selection.Range.Hyperlinks.Count by 1.

To see how the count of links is changing you can run the following method on a document:

Sub AddAndRemoveHyperlink()

    Dim oRange As Range
    Set oRange = ActiveDocument.Range
    oRange.Collapse wdCollapseStart
    oRange.MoveEnd wdCharacter

    Debug.Print ActiveDocument.Range.Hyperlinks.Count

    ActiveDocument.Hyperlinks.Add oRange, "http://www.example.com"
    Debug.Print ActiveDocument.Range.Hyperlinks.Count

    ActiveDocument.Hyperlinks.Item(1).Delete
    Debug.Print ActiveDocument.Range.Hyperlinks.Count

End Sub
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!