Solved

Update HTML links within word documents

Posted on 2011-02-10
4
339 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Word 2016 stuck on "starting" splash screen 10 798
Microsoft Word Pop Up Form 7 34
MailMerge VBA DataSource Connection Issue 10 48
Convert .PDF 6 59
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
Preface: When I started this series, I used the term CommandBars because that is the Office Object class that it discusses. Unfortunately, when Microsoft introduced Office 2007, they replaced the standard Commandbar menus with "The Ribbon" and rem…
In this video, we show how to convert an image-only PDF file into a PDF Searchable Image file, that is, a file with both the image (typically from scanning) and text, which is created in an automated fashion with Optical Character Recognition (OCR) …
This video walks the viewer through the process of creating a watermark for their document, customizing it, and saving it for viewing/printing needs.

914 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

19 Experts available now in Live!

Get 1:1 Help Now