Results 1 to 3 of 3

Thread: Replacing hyperlinks w/ code

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2010
    Posts
    1

    Replacing hyperlinks w/ code

    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.

  2. #2
    Hyperactive Member
    Join Date
    Sep 2009
    Location
    Lost in thought
    Posts
    349

    Re: Replacing hyperlinks w/ code

    Can You just
    Code:
     OldString = "http://site/viewer&wb=Digitizing"
     NewString = "http://site/viewer&wb=AIA_Digitizing"
    
      Open File and get TheStringFromFile
    
          StringToSaveToFile = Replace (TheStringFromFile,OldString,NewString)
    
      Open File and Save (StringToSaveToFile)

  3. #3
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Replacing hyperlinks w/ code

    Quote Originally Posted by stfed View Post
    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.
    You extracted multiple copies of same link from document, but collection keys have to be unique. You have to change your intermediate data store; either (a) use two collections to store unedited and edited links (the items) related via line number in text file (the key), or (b) use a multi-dimension array.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width