Need to replace all filenames with hyperlinks to those files

R

Richard

I have a word docs that contain full filenames.

I need to replace each filename with a hyperlink to that file.
Fortunately each filename is preceeded by a '_' and is followed by a ' |'

e.g. _C:\Documents and Settings\My Documents\filename.txt |
 
D

Doug Robbins - Word MVP

The following code should do it:

Dim drange As Range
Dim LinkAddress As String
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(FindText:="_[A-Z]{1}:", Forward:=True, _
MatchWildcards:=True, Wrap:=wdFindContinue, MatchCase:=False) = True
Set drange = Selection.Range
drange.End = ActiveDocument.Range.End
drange.End = drange.Start + InStr(drange, "|")
LinkAddress = Mid(drange.Text, 2)
LinkAddress = Replace(LinkAddress, "|", "")
LinkAddress = Trim(LinkAddress)
drange.Text = ""
ActiveDocument.Hyperlinks.Add drange, Replace(LinkAddress, "\", "/")
Loop
End With


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 

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