如何从 Microsoft Word 文档中删除超链接? [英] How do you remove hyperlinks from a Microsoft Word document?

查看:29
本文介绍了如何从 Microsoft Word 文档中删除超链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个 VB 宏来为我的工作做一些文档处理.搜索文本行并将括号中的文本放入列表(框)中.

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 when I want to remove all hyperlinks in the document and then generate new ones (not necessarily in the location of the original hyperlinks)

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

So the problem is How do I remove the existing hyperlinks?

我当前的问题是,每次添加链接时,超链接计数都会增加一个,但是当您删除它时,计数不会减少.(因此,我现在有一个包含 32 个链接的文档 - 除了我自己放入的 3 个链接之外都是空的 - 它们没有出现在文档中)

My current issue is that every time a link gets added, the hyperlinks count goes up one, but when you delete it, the count does NOT reduce. (as a result I now have a document with 32 links - all empty except for 3 I put in myself - they do not show up in the document)

代码末尾是我删除超链接的尝试.

At the end of the code are my attempts at removing the hyperlinks.

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 代码以防它对某人有用.

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

这篇关于如何从 Microsoft Word 文档中删除超链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆