I've got a Word document with a bunch of hyperlinks I want to change all at once. I found the following code, the first to extract all the links into a text file, the second to replace the hyperlinks in the word document:
--
Sub FileHyperlinks()
Dim HL As Hyperlink
Dim href As String
Dim f
Set f = CreateObject("Scripting.FileSystemObject").CreateTextFile("c:\hyperlinks.txt", True)
For Each HL In ActiveDocument.Hyperlinks
href = HL.Address
If href <> "" Then f.WriteLine (href)
Next HL
f.Close
End Sub
--
--
Sub ReplaceHyperlinks()
Dim f, A
Dim href, s As String
Dim HL As Hyperlink
Dim HLs As New Collection
Set f = CreateObject("Scripting.FileSystemObject").OpenTextFile("c:\hyperlinks.txt")
Do While f.AtEndOfStream <> True
s = f.Readline
If s <> "" Then A = Split(s, Chr$(9))
If A(1) <> "" Then HLs.Add Item:=A(1), Key:=A(0)
Loop
f.Close
On Error Resume Next
For Each HL In ActiveDocument.Hyperlinks
href = HL.Address
If href <> "" Then
s = HLs.Item(href)
If s <> "" Then HL.Address = s
End If
Next HL
End Sub
--
I then edited the hyperlinks by writing the new one after a tab on each line. Here's an example, w/ the first being the original, and the second being the new hyperlink I want inserted:
http://site/viewer&wb=Digitizing_Univ&ws=Universe http://site/viewer&wb=AIA_Digitizing_Univ&ws=Universe
The problem is I keep getting an error message: Error 457: "This key is already associated with an element in this collection" and debug is highlighting
If A(1) <> "" Then HLs.Add Item:=A(1), Key:=A(0) as the problem.
Can anyone shed some light? I'm not very familiar with visual basic, so the answer might be incredibly simple I realize. Thanks.