turning a document address into a hyperlink

  • Thread starter Thread starter Larry
  • Start date Start date
L

Larry

Is there a way to turn a document path into a hyperlink without recourse
to the Hyperlink dialog box? Let's say I have this address printed in a
document:

C:\Documents\draft.doc

Can I turn that into a hyperlink, the same way that a web address turns
into a hyperlink when I press the Return key or the space key after the
address?

Larry
 
No. You could write a macro to do it, attached to a key combination. But do
you really want to do this? Hyperlinks to hard-coded paths usually cause
more trouble than they're worth.
 
Hi Jezebel,

I'm wondering why it would cause troubles? How is the final result any
different from a hyperlink to a document created with the Hyperlink
dialog box?

Here's a crude macro I put together that does it. It's just a matter of
adding the quotes and the double backslashes to the document path, and
then pasting that path into a hyperlink field.

Selection.Paragraphs(1).Range.Select
' put quotes around path
Application.Run MacroName:="QuoteSelectionOrParagraphToggle"
Selection.Paragraphs(1).Range.Select
' change \ to \\ in path.
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "\"
.Replacement.Text = "\\"
.Forward = True
.Wrap = wdFindStop
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Paragraphs(1).Range.Cut
' create hyperlink field and type in word "hyperlink"
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, _
PreserveFormatting:=False
Selection.TypeText Text:="hyperlink "
' paste the formatted path into the field
Selection.Paste
ActiveWindow.View.ShowFieldCodes = Not
ActiveWindow.View.ShowFieldCodes
Application.Run MacroName:="UpdateAllFields"

Larry
 
No technical problem -- just that documents move so often. Perhaps you can
be confident that your file names and locations are persistent enough for
this to work; I've never seen it work satisfactorily anywhere else.
 
Hi Larry

Try selecting the path and running this macro:

Sub HyperlinkThisPath()
If Selection.Type <> wdSelectionIP Then
ActiveDocument.Hyperlinks.Add _
Anchor:=Selection.Range, _
Address:=Trim(Selection.Text), _
TextToDisplay:=Selection.Text
End If
End Sub

Be aware that there's no error-checking here to make sure the address
is valid; it'll happily try to turn a single word or the Gettysburg
Address into a hyperlink. If you want it to have more smarts, tell me
what criteria you want to apply.
 
Thanks, Jay.

It wouldn't work at first, because "TextToDisplay" doesn't seem to be an
argument available in Word 97, so I just took out that argument, and
then the macro worked.

But how does it work? Does the Hyperlinks.Add method know to turn the
single backslashes in a document path into double backslashes and put
quotes around the path?

Larry
 
But how does it work? Does the Hyperlinks.Add method know

Yes, that's part of the internal code of the method. Look at the code of the
resulting HYPERLINK field, and you'll see that it has the quotes and double
backslashes.
 
Back
Top