View Single Post
  #2   Report Post  
Posted to microsoft.public.word.docmanagement
Jay Freedman Jay Freedman is offline
external usenet poster
 
Posts: 9,854
Default Create hyperlink via macro? Word 2007

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 FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.

EPPack wrote:
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