你如何从一个Microsoft Word文档中删除链接?(How do you remove hyp

2019-07-29 18:55发布

我正在写一个VB宏做的一些文件处理我的工作。 文本的行搜查,括号中的文字放在一个列表(盒)。

问题是当我想删除所有超链接的文档中,然后产生新的(不一定是在原来的超链接的位置)

所以,问题是我如何删除现有的超链接?

我现在的问题是,每一个环节被添加的时候,超链接计数上升一个,但是当你删除它,计数不降低。 (作为一个结果,我现在有32点链接的文档 - 所有空除3我把我自己 - 他们没有在文件中显示出来)

在代码的结尾是我在尝试移除链接。

Private Sub FindLinksV3_Click()

    ListOfLinks.Clear

    ListOfLinks.AddItem Now
    ListOfLinks.AddItem ("Test String 1")

    ListOfLinks.AddItem ActiveDocument.FullName

    SentenceCount = ActiveDocument.Sentences.Count
    ListOfLinks.AddItem ("Sentence Count:" & SentenceCount)
    counter = 0

    For Each myobject In ActiveDocument.Sentences    ' Iterate through each element.
        ListOfLinks.AddItem myobject
        counter = counter + 1

        BracketStart = (InStr(1, myobject, "("))

        If BracketStart > 0 Then
            BracketStop = (InStr(1, myobject, ")"))

            If BracketStop > 0 Then
                ListOfLinks.AddItem Mid$(myobject, BracketStart + 1, BracketStop - BracketStart - 1)

                ActiveDocument.Sentences(counter).Select

                ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, Address:= _
                "http://testnolink/" & counter, ScreenTip:=""  'TextToDisplay:=""

            End If
        End If
    Next

    'ActiveDocument.Sentences(1).Select
    '
    'Selection.Range.Hyperlinks(1).Delete

    ActiveDocument.Hyperlinks.Item(1).Delete

    Debug.Print ActiveDocument.Hyperlinks.Count

End Sub

Answer 1:

这是一个古老的职位,所以我将在此情况下,VBA代码是有用的人。

超链接(集合)需要以相反的顺序被删除:

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


Answer 2:

该行移除超链接被注释掉。 下面线将移除所选范围内的第一超链接:

Selection.Range.Hyperlinks(1).Delete

这也将递减Selection.Range.Hyperlinks.Count 1。

要了解如何链接的数量正在改变,你可以运行一个文件下面的方法:

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


文章来源: How do you remove hyperlinks from a Microsoft Word document?