?
Solved

Update HTML links within word documents

Posted on 2011-02-10
4
Medium Priority
?
346 Views
Last Modified: 2013-11-25
I have a situation whereby somebody has been employed to edit multiple policy and procedure documents (doc format), but have in doing so buggered up all of the html linking (to the tune of 600 documents)

I had some code from here and I thought it worked, but it just rewrote the text, not the underlying link.

Is there a way for me to create an app or macro to go through a list (1 column current link, 2nd column correct link) and then update a whole bunch of documents as a batch?  
0
Comment
Question by:techvagabond
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 37

Accepted Solution

by:
TommySzalapski earned 2000 total points
ID: 34875054
Yes. That only searches the text. It needs to be modified to search the addresses in the hyperlinks. Note: this code is not case sensitive but it does need to be fairly exact otherwise.
Option Explicit
Sub BatchReplaceFromTableList()
Dim strFilename As String
Dim strPath As String
Dim oChanges As Document, oDoc As Document
Dim oTable As Table
Dim rFindText As Range, rReplacement As Range
Dim i As Long
Dim fDialog As FileDialog
Dim sFname As String
Dim link As Hyperlink

'Change the path in the following line to reflect the name and location of the table.
sFname = "C:\temp\Changes.doc"
If Documents.Count > 0 Then
    Documents.Close SaveChanges:=wdPromptToSaveChanges
End If
Set oChanges = Documents.Open(FileName:=sFname, Visible:=True)
Set oTable = oChanges.Tables(1)
Set fDialog = Application.FileDialog(msoFileDialogFolderPicker)
With fDialog
    .Title = "Select folder and click OK"
    .AllowMultiSelect = False
    .InitialView = msoFileDialogViewList
    If .Show <> -1 Then
        MsgBox "Cancelled By User"
        Exit Sub
    End If
    strPath = fDialog.SelectedItems.Item(1)
    If Right(strPath, 1) <> "\" Then
        strPath = strPath + "\"
    End If
End With

If Left(strPath, 1) = Chr(34) Then
    strPath = Mid(strPath, 2, Len(strPath) - 2)
End If
strFilename = Dir$(strPath & "*.doc")

'Convert to lowercase so comparison will not be case sensitive
For i = 1 To oTable.Rows.Count
    oTable.Cell(i, 1).Range = LCase(oTable.Cell(i, 1).Range)
Next i

While Len(strFilename) <> 0
    Set oDoc = Documents.Open(strPath & strFilename)
    For Each link In oDoc.Hyperlinks
        For i = 1 To oTable.Rows.Count
            If LCase(link.Address) Like Left(oTable.Cell(i, 1).Range, Len(oTable.Cell(i, 1).Range) - 3) & "*" Then
                link.Address = oTable.Cell(i, 2).Range
            End If
        Next i
    Next
    oDoc.Close SaveChanges:=wdSaveChanges
    strFilename = Dir$()
Wend
oChanges.Close wdDoNotSaveChanges
End Sub

Open in new window

0
 
LVL 4

Author Comment

by:techvagabond
ID: 34901628
The code doesn't seem to do the rick, but looking through it, I cannot tell what possibly, I am doing wrong...
0
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 34901707
Do the lists have all the right stuff in them so that they match exactly like http:// in the replace table?
Under line 47 add this line
MsgBox link.Address
And look at exactly how the link appears. Your replacement table should match
(By the way, if you hit the break button on your keyboard (or ctrl+break) then you can stop the code instead of having it display a message box for every singe link).
0
 
LVL 4

Author Closing Comment

by:techvagabond
ID: 34947941
Champion, it was in fact something I was doing, there was a line break in a table row, it was causing an error and deleting all of the links out of the original changes.doc file.

Your help is very much appreciated.

Cheers
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
This video shows the viewer how to set up and create Footnotes in their document. Click on the References tab: Select "Insert Footnote": Type in desired text:
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question