an alternative way of creating a hyperlink from one document to another

L

Larry

If the document I want to create a link to is in another folder from the
present document, then, using the Insert Hyperlink Dialog box, I have to
browse for that document. Here is a quicker way to create a hyperlink
to another document, if the the other is already open in Word. I simply
open that document and run the below macro which creates a hyperlink to
that document and cuts the hyperlink to the Clipboard. Then I switch
back to the first document and paste the hyperlink. This is faster and
easier than opening the Insert Hyperlink dialog and browsing through
several folders.

Sub HyperlinkToThisDoc()
' by Larry

' Creates a hyperlink to active document and cuts it to Clipboard
' for insertion into another document.

' Set it up so document won't have to be saved afterward.
Dim bSaveState As Boolean
bSaveState = ActiveDocument.Saved

Dim r As Range
Set r = Selection.Range

Application.ScreenUpdating = False

' Go to end of document, type paragraph.
Selection.EndKey wdStory
Selection.TypeParagraph

' Print current document path.
Selection.TypeText ActiveDocument.FullName

' Select the doc path without the following para mark.
Selection.Paragraphs(1).Range.Select
Selection.MoveEndWhile cset:=Chr(13), Count:=wdBackward
' Make path small size so it won't run to two lines
' (though that doesn't matter)
With Selection.Font
.Name = "Verdana"
.Size = 8
End With

' Turn the selected document path
' into a hyperlink.

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

' Select the hyperlink, unselect para mark, and cut selection.
Selection.Paragraphs(1).Range.Select
Selection.MoveEndWhile cset:=Chr(13), Count:=wdBackward
Selection.Cut
Selection.Font.Reset
Selection.TypeBackspace

' return cursor to starting point.
r.Select
' Return document to the saved state it was in at start of macro.
ActiveDocument.Saved = bSaveState

End Sub
 
L

Larry

Sorry for the typo. I meant to write:

Here is a quicker way to create a hyperlink to another document, if the
other document is already open in Word.
 
L

Larry

I made a slight change to re-apply the Hyperlink style to the hyperlink
so that it will have the correct font characteristics.


Sub HyperlinkToThisDoc()
'
' Creates a hyperlink to current document and cuts it to Clipboard for
insertion
' into another document.

' Set it up so document won't have to be saved afterward.
Dim bSaveState As Boolean
bSaveState = ActiveDocument.Saved

Dim r As Range
Set r = Selection.Range

Application.ScreenUpdating = False

' Go to end of doc, type paragraph.

Selection.EndKey wdStory
Selection.TypeParagraph

' Print current document path.
Selection.TypeText ActiveDocument.FullName
' Select the doc path without the following para mark.
Selection.Paragraphs(1).Range.Select
Selection.MoveEndWhile cset:=Chr(13), Count:=wdBackward
' Make path small size so it won't run to two lines (though that doesn't
matter)
With Selection.Font
.Name = "Verdana"
.Size = 8
End With

' Run code that turns the selected printed doc path
' into a hyperlink.

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

' Select the hyperlink, unselect para mark, and cut selection.
Selection.Paragraphs(1).Range.Select
Selection.MoveEndWhile cset:=Chr(13), Count:=wdBackward
Selection.Style = ("Hyperlink")
Selection.Cut
Selection.Font.Reset
Selection.TypeBackspace

r.Select
ActiveDocument.Saved = bSaveState

End Sub
 

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