Modifying Hyperlinks

S

Steve Coates

Does anybody know if it is possible to modify hyperlinks
in a Word document using VBA and if so how?
I have a series of docs that contain lots of links only
the server address has changed and I need to change them
all in one fell swoop. The plain vanilla Find and Replace
dialog box does not work because it is the underlying
Hyperlink object that I need to get at rather than the
icing on top.
I've seen snippets of code that create hyperlinks but none
that actually retrieve existing and change them. Excel was
a doddle but I can't find an answer for Word.

Thanks.
 
D

Doug Robbins - Word MVP

Hi Steve,

You could modify the following to deal with hyperlinks

' Macro created 26/10/01 by Doug Robbins to update links in a document
'
Dim alink As Field, linktype As Range, linkfile As Range
Dim linklocation As Range, i As Integer, j As Integer, linkcode As Range
Dim Message, Title, Default, Newfile
Dim counter As Integer


counter = 0
For Each alink In ActiveDocument.Fields
If alink.Type = wdFieldLink Then

Set linkcode = alink.Code
i = InStr(linkcode, Chr(34))
Set linktype = alink.Code
linktype.End = linktype.Start + i
j = InStr(Mid(linkcode, i + 1), Chr(34))
Set linklocation = alink.Code
linklocation.Start = linklocation.Start + i + j - 1
If counter = 0 Then
Set linkfile = alink.Code
linkfile.End = linkfile.Start + i + j - 1
linkfile.Start = linkfile.Start + i
Message = "Enter the modified path and filename following this
Format " & linkfile
Title = "Update Link"
Default = linkfile
Newfile = InputBox(Message, Title, Default)
End If
linkcode.Text = linktype & Newfile & linklocation
counter = counter + 1
End If
Next alink

And then, you could graft it into the code that you will find in the article
"How to Find & ReplaceAll on a Batch of Documents in the Same Folder" at:

http://www.mvps.org/word/FAQs/MacrosVBA/BatchFR.htm

Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a consulting basis.

Hope this helps
Doug Robbins - Word MVP
 
S

Steve Coates

Thanks for the prompt response Doug.

I grafted the code as you suggested and tested. It worked
great except that I forgot to mention that I only wanted
part of the hyperlink changed.
Each link references a different file on the server eg. I
need to change from
"http://docushare.nzei.org.nz/dscgi/ds.py/Get/File-12345"
to
"http://docushare/docushare/dsweb/Get/Document-12345"
then the next link might be pointing at File-98765
In other words I need to preserve the last 5 characters of
each string and add it to the new string then save it.

Thanks

Steve
 
S

Steve Coates

Hi Doug

Brilliant! With a bit of tweaking I got them all changed.
For the benefit of anyone else interested the main changes
I had to make were:
1. Comment out Dialogs(wdDialogEditReplace).show cos this
wan't needed.
2.If alink.type = wdFieldlink was returning 88 when
wdFieldlink = 56 so I just changed it to If alink.type = 88
3. Based on your latest suggestion I added the following
before If counter = 0:
linklocation.Text = "Document-" & Right(linkcode, 7)
I actually had to test the length of the link because some
of the file handles were 4 digits and others 5. A simple
if then statement then applied.

Once again, many thanks.

Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top