Create hyperlink via macro? Word 2007

E

EPPack

Having a problem creating a macro to insert a hyperlink in Word 2007. Here's
exactly what I want it to do:

1. AFTER I highlight a few words or so in the document, I press the
button/hotkeys that kicks off the macro
2. The Insert Hyperlink box comes up
3. The address bar gets prefilled with EXACTLY the same text (a lengthy,
unchanging path to a file) EVERY TIME, then the macro STOPS or PAUSES running
so that the user can enter a specific file name, which will change each time,
thus completing the full path.
4. I click on OK and the hyperlink is created correctly.
5. We only see the clickable text in the document, as a link to the fully
qualified file name as entered, NOT the actual hyperlink code.

The problem:

When I recorded the macro initially, I typed in the path string uneventfully
but there's no way to stop or pause the recording at this point, because it's
in a dialog box. If I let it finish, because it never paused to let me enter
a file name, there's not one in the resulting hyperlink. In addition, because
it also captured the highlighted text, even tho it was highlighted before
starting to record, running this macro a second time replaces the new
highlighted text with the highlighted text from the previous hyperlink.

I believe that with judicious editing of the macro in VBA maybe this can all
be done, but I'm simply not familiar enough with the code to do that. This is
the code it generated:

Sub HyperlinkPath()
' HyperlinkPath Macro
ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, Address:= _

"http://www.albemarle.org/upload/images/forms_center/board_of_supervisors/forms/agenda/2010files/" _
, SubAddress:="", ScreenTip:="", TextToDisplay:= _
"First Highlighted Text"

Is this even doable? If so, can someone point me to a solution?

TIA

elaine
charlottesville, va
 
J

Jay Freedman

Use the following macro. Besides using the current selected text as the
hyperlink's display text, it also does some error checking to make sure the
selection is valid and that the user has entered something in the input box.
The underscores at the ends of some lines indicate that the statement
continues on the next line; the space before the underline is required.

Sub HyperlinkPath()
Const Path = "http://www.albemarle.org/upload/images/" & _
"forms_center/board_of_supervisors/forms/agenda/2010files/"
Dim displayText As String
Dim addressText As String
Dim fileName As String

If Selection.Type <> wdSelectionNormal Then
MsgBox prompt:="Please select some text.", _
buttons:=vbOKOnly + vbCritical, Title:="Invalid Selection"
Exit Sub
End If

fileName = InputBox("Enter file name:", "Hyperlink Path")
If Len(fileName) = 0 Then Exit Sub 'canceled or didn't enter anything

addressText = Path & fileName
displayText = Selection.Text

ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, _
Address:=addressText, TextToDisplay:=displayText
End Sub


--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
P

Pesach Shelnitz

Hi Elaine,

The following revised version of your macro works for me.

Sub HyperlinkPath()
' HyperlinkPath Macro

Dim UrlPath As String
Dim FileName As String

UrlPath = "http://www.albemarle.org/upload/images/" _
& "forms_center/board_of_supervisors/forms/agenda/2010files/"
FileName = InputBox("Type the file name.", "File Name")
ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, _
Address:=UrlPath & FileName
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