Problem with Hyperlink coding

L

Lee

Hi all
I have the following code which opens a dialog box and the
users can choose a document to link to BUT once the
document is clicked on and the user presses OK, the file
name of the document is not inserted
(TextToDisplay:=Selection.Text). HELP!

Sub Hyperlink()

Dim fd As FileDialog
'Create a FileDialog object as a File Picker dialog
box.
Set fd = Application.FileDialog
(msoFileDialogFilePicker)
'Use a With...End With block to reference the
FileDialog object.
With fd
'Set the initial path to the Agenda Attachments
folder.
.InitialFileName
= "\\alchemy\data\processes\documents\agenda attachments\"
.Title = "Select the File to which you want to
create the link"
'Use the Show method to display the File Picker
dialog box and return the user's action.
'If the user presses the action button...
If .Show = -1 Then
ActiveDocument.Hyperlinks.Add
Anchor:=Selection.Range, Address:=.SelectedItems(1),
TextToDisplay:=Selection.Text
' 'If the user presses Cancel...
Else
End If
End With

'Set the object variable to Nothing.
Set fd = Nothing

End Sub

Thanks to all who helped with this code in the first
instance.

Lee
 
D

Doug Robbins - Word MVP

I assume that you want the filename to be displayed. If that's the case,
use:

Dim fd As FileDialog, displaytext As String
'Create a FileDialog object as a File Picker dialog box.
Set fd = Application.FileDialog(msoFileDialogFilePicker)
'Use a With...End With block to reference the FileDialog object.
With fd
'Set the initial path to the Agenda Attachments folder.
' .InitialFileName = "\\alchemy\data\processes\documents\agenda
attachments\"
.Title = "Select the File to which you want to create the link"
'Use the Show method to display the File Picker dialog box and
return the user's action.
'If the user presses the action button...
If .Show = -1 Then
displaytext = .SelectedItems(1)
While InStr(displaytext, "\") > 0
displaytext = Mid(displaytext, InStr(displaytext, "\") + 1)
Wend
displaytext = Left(displaytext, Len(displaytext) - 4)
'ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range,
Address:=.SelectedItems(1), TextToDisplay:=displaytext
' 'If the user presses Cancel...
Else
End If
End With

'Set the object variable to Nothing.
Set fd = Nothing

If you want the complete path to be displayed, you can dispense with the
displaytext and just use

TextToDisplay: = .SelectedItems(1)
--
Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
 
L

Lee

Thanks a lot Doug. This works exactly how I want it to. I
really appreciate your help... and yes, I wanted the
filename to display... Thank you
Lee
-----Original Message-----
I assume that you want the filename to be displayed. If that's the case,
use:

Dim fd As FileDialog, displaytext As String
'Create a FileDialog object as a File Picker dialog box.
Set fd = Application.FileDialog (msoFileDialogFilePicker)
'Use a With...End With block to reference the FileDialog object.
With fd
'Set the initial path to the Agenda Attachments folder.
' .InitialFileName
= "\\alchemy\data\processes\documents\agenda
 

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