Link to home
Start Free TrialLog in
Avatar of Richard Detsch
Richard Detsch

asked on

Replace filenames with hyperlinks to the filenames in Word

I have a word docs that contain a number of full filenames,

I need to replace each filename in a MS Word document 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 |

The text associated with the hyperlink should be the filename
Avatar of bcoyxp
bcoyxp
Flag of Saudi Arabia image

hi there,
f
or fast editing with the "_" and "|" chars,

you may use the MSword's "Find and Replace" wizard.

Press "cntrl+h" to open the wizard.

find "_c:" and Replace with "c:" -> select "replace all"
you may do the same sequence with the other character.

for hyper linking, use the quotation marks.

e.g.

C:\Documents and Settings\My Documents\filename.txt

change to

"C:\Documents and Settings\My Documents\filename.txt" then press enter,

MSword will automatically change it into a hyperlink.


Regards,
Avatar of Richard Detsch
Richard Detsch

ASKER

Didn't find your answer useful.
I was looking for a MS Word Macro to do this; perhaps I should have been more clear on this.
Avatar of irudyk
Try the following macro and see if it works out for you:
Sub CreateHyperlinks()
 
Application.ScreenUpdating = False
 
With ActiveWindow.Selection
 
    .HomeKey wdStory
 
    With .Find
        .ClearFormatting
        .Text = "_*|"
        .Forward = True
        .Wrap = wdFindStop
        .MatchWildcards = True
        .Execute
    End With
 
    While .Find.Found
        ActiveDocument.Range(.Start + 1, .End - 1).Hyperlinks.Add ActiveDocument.Range(.Start + 1, .End - 1), ActiveDocument.Range(.Start + 1, .End - 1).Text
        .Collapse wdCollapseEnd
        .Find.Execute
    Wend
 
End With
 
Application.ScreenUpdating = True
 
End Sub

Open in new window

irudyk:
Wow, very clean script.
I still have one problem though. My filenames start with _\\ERD  not just _
And there are other occurances of _*|   so your code  picks up non-filenames too.

I tried to change your search to "_\\ERD*|", but discovered that '\' is itself part of a wildcard search.
[FROM MS HELP: To search for a character that's defined as a wildcard, type a backslash (\) before the character. For example, type \? to find a question mark. ]

How do I update my find criteria to pick up only  _\\ERD...rest of filename... |

PS. What does ".HomeKey wdStory" do?
ASKER CERTIFIED SOLUTION
Avatar of irudyk
irudyk
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
irudyk:
Well done. All set.