|
|
|
|
|
|
Excel
|
Insert a hyperlink to a Word Bookmark
|
|
Ease of Use
|
Easy
|
Version tested with
|
2002, 2003
|
Submitted by:
|
Killian
|
Description:
|
Allows the user to insert a hyperlink in the active cell, chosen from a list of all the bookmarks in a nominated Word document.
|
Discussion:
|
While Excel has a native "Insert Hyperlink" feature, it only allows a file path to other valid document types (such as a Word doc). And it's "Bookmarks" option only allows links to sheets, cells and named ranges within the active workbook.
This example provides a similar interface to select bookmarks in a nominated Word document, using the HYPERLINK worksheet formula so can be used for creating linked references to different sections of a word document(s).
|
Code:
|
instructions for use
|
Option Explicit
Sub InsertHyperLinkToWordRange()
Dim appWord As Word.Application
Dim docTarget As Word.Document
Dim objBookmark As Word.Bookmark
Dim varWordFile As Variant
varWordFile = Application.GetOpenFilename( _
fileFilter:="Word Documents (*.doc), *.doc")
If Not varWordFile = False Then
Set appWord = New Word.Application
With appWord
.Visible = False
Set docTarget = .Documents.Open(varWordFile)
Load frmInsertWordHL
frmInsertWordHL.lblDocPath.Caption = docTarget.FullName
For Each objBookmark In docTarget.Bookmarks
frmInsertWordHL.lstBookmarks.AddItem objBookmark.Name
Next
docTarget.Close False
.Quit
Set docTarget = Nothing
Set appWord = Nothing
frmInsertWordHL.Show
End With
Else
MsgBox "No file was chosen", vbExclamation, "Insert HyperLink - File check"
End If
End Sub
Option Explicit
Private Sub lstBookmarks_Click()
txtDisplayText = lstBookmarks.Value
End Sub
Private Sub cmdInsert_Click()
ActiveCell.Formula = "=HYPERLINK(" & """" & "[" & lblDocPath.Caption & _
"]" & lstBookmarks.Value & """" & "," & """" & "Link to " & txtDisplayText & _
"""" & ")"
Unload Me
End Sub
Private Sub cmdCancel_Click()
Unload Me
End Sub
|
How to use:
|
- In a new workbook, Alt + F11 to enter the VBE
- From VBE>Tools>References, add a reference to the Microsoft Word Object Library
- Choose Insert>Module
- Paste the section of code marked 'Code for standard module' into the code pane
- Choose Insert>UserForm
- Change the "Name" property of the userform to 'frmInsertWordHL'
- Choose View>Toolbox
- Using the Toolbox menu:
- add a listbox to the userform - change it's name property to 'lstBookmarks'
- add a textbox to the userform - change it's name property to 'txtDisplayText'
- add a commandbutton to the userform - change it's name property to 'cmdInsert'
- add a commandbutton to the userform - change it's name property to 'cmdCancel'
- Paste the section of code marked 'Code for userform' into the code pane
- Press Alt + Q to close the VBE and save the workbook
- Open Word and create a test document that contains at least one bookmark
- Save the test document and close Word
|
Test the code:
|
- Press Alt + F8, select 'InsertHyperLinkToWordRange', click Run
- Word's FileOpen dialog box will be displayed, navigate to your test Word document and click 'Open'
- The userform will be displayed, select a bookmark from the listbox
- The name displayed in the hyperlink will default to the bookmark name - this can be changed in the text field below the listbox
- Click 'Insert'
- Click on the new hyperlink - this should link to the Word document and display the bookmarked range selected
|
Sample File:
|
InsertHyperLinkToWordBookMark.zip 17.93KB
|
Approved by mdmackillop
|
This entry has been viewed 154 times.
|
|