Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 314
  • Last Modified:

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
0
Richard Detsch
Asked:
Richard Detsch
  • 3
  • 2
1 Solution
 
bcoyxpCommented:
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,
0
 
Richard DetschChief Knowledge OfficerAuthor Commented:
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.
0
 
irudykCommented:
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

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Richard DetschChief Knowledge OfficerAuthor Commented:
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?
0
 
irudykCommented:
Hmm, okay well try the following code.
The .HomeKey wdStory sets the postion of the cursor to the start of the document

Sub CreateHyperlinks()
 
Application.ScreenUpdating = False
 
With ActiveWindow.Selection
 
    .HomeKey wdStory
 
    With .Find
        .ClearFormatting
        .Text = "_[^092^092ERD]*|"    'to find a \ use ^nnn where nnn is the ASCII code for the character (in this case, \ ASCII code = 092)
        .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

0
 
Richard DetschChief Knowledge OfficerAuthor Commented:
irudyk:
Well done. All set.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now