vba 如何从 Microsoft Word 文档中删除超链接?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1091785/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you remove hyperlinks from a Microsoft Word document?
提问by Dirk Vollmar
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).
我正在编写一个 VB 宏来为我的工作处理一些文档。搜索文本行并将括号内的文本放入列表(框)中。
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?
所以问题是如何删除现有的超链接?
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)
我当前的问题是,每次添加链接时,超链接计数都会增加一,但是当您删除它时,计数不会减少。(因此,我现在有一个包含 32 个链接的文档——除了我自己输入的 3 个链接之外,所有链接都是空的——它们没有出现在文档中)
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
回答by Mike Whyte
This is an old post, so am adding this VBA code in case it is useful to someone.
这是一篇旧帖子,所以我添加了这个 VBA 代码,以防它对某人有用。
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
回答by Dirk Vollmar
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.
这也将减少Selection.Range.Hyperlinks.Count
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