How do you remove hyperlinks from a Microsoft Word document?

前端 未结 2 569
挽巷
挽巷 2021-01-25 06:45

I\'m writing a VB Macro to do some processing of documents for my work. The lines of text are searched and the bracketed text is put in a list(box).

The problem comes w

相关标签:
2条回答
  • 2021-01-25 07:11

    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
    
    0 讨论(0)
  • 2021-01-25 07:18

    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
    
    0 讨论(0)
提交回复
热议问题