Solved

Update HTML links within word documents

Posted on 2011-02-10
4
342 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
  • 2
  • 2
4 Comments
 
LVL 37

Accepted Solution

by:
TommySzalapski earned 500 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: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying 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

Nice table. Huge mess. Maybe this was something you created way back before you figured out tabs or a document you received from someone else. Either way, using the spacebar to separate the columns resulted in a mess. Trying to convert text to t…
I would like to show you some basics you can do with Mailings in MS Word. It´s quite handy feature you can use for creating envelopes, labels, personalized letters etc. First question could be what is this feature good for? Mailing can really he…
This video teaches the viewer how to align pictures around text while keeping the text properly aligned in the document.
This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …

839 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