Solved

Update HTML links within word documents

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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Winword doc from template - use the template for multiple pages 3 40
Convert .PDF 6 66
What namespace do I need to import? 2 29
Word Perfect Question 11 20
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
Introduction This tutorial provides instructions on how to properly format your Word document using the inbuilt tools provided. The benefits of using these tools means your documents are more accessible and easily portable to other applications an…
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 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:

803 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