L
Larry
A couple of weeks ago Jay Freedman showed me this code which turns a
path of a file into a hyperlink.
If Selection.Type <> wdSelectionIP Then
ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, _
Address:=Trim(Selection.Text)
End If
Now I'd like something related but more complicated.
Often in Word I'll want to apply a web address to a word or words in a
document. To do this manually, I first paste in the web address near
the target text, then press space bar to turn the web address into a
hyperlink, then I cut the target text and paste it (unformatted paste)
onto the hyperlink. The result is the text, hyperlinked to the web
address.
I'd like a simpler way to do that. I came up with a macro, but it's
clunky. I'm wondering if there's a statement hidden away in VBA that
handles this so that I don't waste my time tweaking this macro.
Thanks,
Larry
' retract from any empty space.
Selection.MoveEndWhile cset:=" ", Count:=wdBackward
ActiveDocument.Bookmarks.Add Range:=Selection.Range,
Name:="TestForHyperlink"
With Selection
.Collapse wdCollapseEnd
.MoveEndWhile cset:=" "
.Collapse wdCollapseEnd
Application.Run MacroName:="Template1.Module1.PasteAndSelect"
Application.Run MacroName:="HyperlinkFromPath"
.TypeText Text:=" "
.GoTo What:=wdGoToBookmark, Name:="TestForHyperlink"
.Cut
.Characters.First.Previous.Delete
.MoveRight wdWord, 1
.MoveRight wdWord, 1, wdExtend
End With
PasteUnformatted
path of a file into a hyperlink.
If Selection.Type <> wdSelectionIP Then
ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, _
Address:=Trim(Selection.Text)
End If
Now I'd like something related but more complicated.
Often in Word I'll want to apply a web address to a word or words in a
document. To do this manually, I first paste in the web address near
the target text, then press space bar to turn the web address into a
hyperlink, then I cut the target text and paste it (unformatted paste)
onto the hyperlink. The result is the text, hyperlinked to the web
address.
I'd like a simpler way to do that. I came up with a macro, but it's
clunky. I'm wondering if there's a statement hidden away in VBA that
handles this so that I don't waste my time tweaking this macro.
Thanks,
Larry
' retract from any empty space.
Selection.MoveEndWhile cset:=" ", Count:=wdBackward
ActiveDocument.Bookmarks.Add Range:=Selection.Range,
Name:="TestForHyperlink"
With Selection
.Collapse wdCollapseEnd
.MoveEndWhile cset:=" "
.Collapse wdCollapseEnd
Application.Run MacroName:="Template1.Module1.PasteAndSelect"
Application.Run MacroName:="HyperlinkFromPath"
.TypeText Text:=" "
.GoTo What:=wdGoToBookmark, Name:="TestForHyperlink"
.Cut
.Characters.First.Previous.Delete
.MoveRight wdWord, 1
.MoveRight wdWord, 1, wdExtend
End With
PasteUnformatted