【问题标题】:How do you remove hyperlinks from a Microsoft Word document?如何从 Microsoft Word 文档中删除超链接?
【发布时间】:2010-11-08 16:08:31
【问题描述】:

我正在编写一个 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

【问题讨论】:

    标签: vba hyperlink listbox ms-word


    【解决方案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
    

    【讨论】:

      【解决方案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
      

      【讨论】:

      • 首先,感谢 divo 纠正了我在发布代码时造成的混乱。我自己非常感激。好的,我是否对 ActiveDocument.Hyperlinks.Count 做了太多的处理? (我真的希望能够拥有一个可以将各种来源粘贴到其中的主文档,这可能会经常更新)如果我取消注释 Selection.Range.Hyperlinks(1).Delete 行,它仍然会失败并出现错误 Runtime error "4198 ”,我将此归因于超链接(1)已在今天早上早些时候被删除......但我的 ActiveDocument.Hyperlinks.Count 现在是 49,(请参阅上面关于这是一个大问题的评论吗?)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-18
      相关资源
      最近更新 更多