Hyperlink Macro in word 2013

I was trying to record a macro in word 2013 that would automatically insert a copied hyperlink into selected text on click of a button.

The ideal operation would be:

  1. Copy hyperlink from browser.
  2. Select text in Word
  3. Click macro button to insert copied hyperlink

Essentially, what I'm trying to do is reduce "Ctrl+K - Ctrl+V - Enter" to a button.  The macro recorded as follows:

Sub Hyperlink()

'
' Hyperlink Macro
'
'
Selection.MoveLeft Unit:=wdCharacter, Count:=5, Extend:=wdExtend
ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, Address:= _
"http://www.greenoptimistic.

" _
, SubAddress:="", ScreenTip:="", TextToDisplay:="test"
End Sub

I attempted to record the macro after selecting some random text, but all it does when I try to run the macro is use the original information from when I recorded the macro.


I hope my question makes sense. Thanks ahead for your answers.

See the "This is how to get the text on the clipboard into a string variable" part of the article "Manipulating the clipboard using VBA” at:

http://www.word.mvps.org/FAQs/MacrosVBA/ManipulateClipboard.htm

What you would then do is use that code in your macro to get the URL from the clipboard into a string variable and then replace the http://www.greenoptimistic in your recorded macro with the string variable.

Hope this helps,
Doug Robbins - MVP Office Apps & Services (Word)
dougrobbinsmvp@gmail.com
It's time to replace ‘Diversity, Equity & Inclusion’ with ‘Excellence, Opportunity & Civility’ - V Ramaswamy

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

Thank you very much for your reply, but I'm afraid it's too technical for me. :/ My expertise lies in other fields.

I guess I'll just stick with Ctrl+K, Ctrl+V, ↵ for now. It's only three keystrokes :)

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

There's really no way to simply put keystrokes in a macro? :/ Oh well, back to AutoHotKey 

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

I was trying to do something very similar but with an extra step. Office programs convert URLs into hyperlinks very easily, like with a space after the address. But they don't do this if the address is to a file path. I send file paths to colleagues frequently, so I wanted to make it easy to convert the text of a file path into a hyperlink to the file or folder. So my three steps using keyboard shortcuts are Ctrl + C, Ctrl + K, Ctrl +V, Enter. At first, I tried to achieve this by following the method recommended by Doug Robbins on this thread but adding Selection.Copy to the beginning. But I couldn't figure out how to get the coding to work for the Address field in the hyperlink code.

Fortunately for me, there's a simpler way to do this. Just use "Selection.Range" wherever you want to use it.

Here's what that looks like:

ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, Address:=Selection.Range, SubAddress:="", ScreenTip:="", TextToDisplay:=Selection.Range

This turns the highlighted text into a hyperlink with an address identical to the highlighted text.

Still, I'd like to see what the code Doug Robbins suggests would actually look like. I tried this, which didn't work:

ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, Address:=Dim MyData As DataObject
Dim strClip As String
Set MyData = New DataObject
MyData.GetFromClipboard
strClip = MyData.GetText, SubAddress:="", ScreenTip:="", TextToDisplay:="test"

This always came up with a "bad parameter" error. I'm not very advanced at this but understand the basics. I'm not sure how to make that clipboard coding work in the Address: or TextToDisplay: fields and would appreciate someone actually posting what they hyperlink language would look like to dump whatever's in the clipboard into the address, no matter what is selected.

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

Use:

Dim MyData As DataObject
Dim strClip As String
Set MyData = New DataObject
MyData.GetFromClipboard
strClip = MyData.GetText

ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, Address:=strClip, SubAddress:="", ScreenTip:="", TextToDisplay:="test"

Hope this helps,
Doug Robbins - MVP Office Apps & Services (Word)
dougrobbinsmvp@gmail.com
It's time to replace ‘Diversity, Equity & Inclusion’ with ‘Excellence, Opportunity & Civility’ - V Ramaswamy

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

Perfect! I replaced "test" with Selection.Range to keep the selected text. That's great. Thanks!

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

 
 

Question Info


Last updated November 26, 2021 Views 3,377 Applies to: