Solved

Update HTML links within word documents

Posted on 2011-02-10
4
338 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Shortcuts in Word Just the other day I had a training for Microsoft and they wanted me to show how well the new Windows and Office behaved on a touch device, which by the way is great, but it was only then that I realized that using keyboard shortc…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This video shows where to find templates, what they are used for, and how to create and save a custom template using Microsoft Word.
Office 365 is currently available in five editions. Three of them are for business use: Office 365 Business Essentials, Office 365 Business, and Office 365 Business Premium. Two of them are for home/personal use: Office 365 Home and Office 365 Perso…

743 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now