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
2 个解决方案
#1
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
#2
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
#1
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
#2
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