Please Note: This article is written for users of the following Microsoft Word versions: 2007, 2010, 2013, 2016, 2019, 2021, 2024, and Word in Microsoft 365. If you are using an earlier version (Word 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Word, click here: Changing What Is Pasted in a Dialog Box.
Ihor wants to automate the inserting of a URL hyperlink into a Word document. The URL will be associated with a phrase, such as "click here". He first copies the URL of a specific website to the Clipboard. He then records a macro that opens the Insert Hyperlink dialog box (Ctrl+K) and pastes into the appropriate field the URL from the Clipboard (Ctrl+V) and clicks OK. When he later runs the macro, it gives him the same URL every time he runs it. Ihor wants to paste a different URL into the dialog box every time he runs the macro, but seems to be missing how to do that.
When you record a macro, it is very literal about what it does—it records exactly the steps you take, including how dialog boxes are filled out. The solution isn't to look for ways to paste new information into a dialog box, but to look at how you are creating your hyperlink. Here's what would be recorded if you inserted a hyperlink with the macro recorder running:
Sub Macro1()
'
' Macro1 Macro
'
'
ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, Address:= _
"http://www.tips.net/", SubAddress:="", ScreenTip:="", TextToDisplay:= _
"click here"
End Sub
What Ihor wants to change is the target for the hyperlink, which is assigned to the Address property; this is what gets "pasted" into the Address field of the dialog box. In order to do this, you could change your macro in a simple manner, such as this:
Sub Macro2()
Dim sTemp As String
sTemp = "http://www.tips.net/"
ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, _
Address:= sTemp, SubAddress:="", ScreenTip:="", _
TextToDisplay:= "click here"
End Sub
All that has been done in this example is delete some of the unnecessary comments at the beginning of the macro and create a string variable, sTemp, that now contains the target for the hyperlink. This variable is then assigned to the Address property. In order to change the target, then, one only needs to change the value of the sTemp variable—and there are a number of ways this can be done.
One way is to use an InputBox function to create your own dialog box, in this manner:
Sub Macro3()
Dim sTemp As String
Dim sPrompt As String
Dim sTitle As String
sPrompt = "Enter the target for the hyperlink"
sTitle = "Hyperlink Destination"
sTemp = "http://www.tips.net/"
sTemp = InputBox(sPrompt, sTitle, sTemp)
ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, _
Address:= sTemp, SubAddress:="", ScreenTip:="", _
TextToDisplay:= "click here"
End Sub
Of course, Ihor mentioned that in his process he actually copies the URL to the Clipboard. If that is the process that he wants to use, it is possible to assign the URL based on whatever is in the Clipboard when the macro is run. Here's how you would do that:
Sub Macro4()
Dim sTemp As String
Dim MyData As DataObject
Set MyData = New DataObject
MyData.GetFromClipboard
sTemp = Trim(MyData.GetText(1))
ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, _
Address:= sTemp, SubAddress:="", ScreenTip:="", _
TextToDisplay:= "click here"
End Sub
In order to utilize the Clipboard in this manner, you'll need to set up a reference for the Microsoft Forms in the VBA Editor. (Choose References from the Tools menu in the Editor.)
Note, as well, that all these examples modify what is assigned to the Address property of your new hyperlink. There is a good chance that you'll want to change the macro to modify what is assigned to the TextToDisplay property, as well.
Note:
WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (11906) applies to Microsoft Word 2007, 2010, 2013, 2016, 2019, 2021, 2024, and Word in Microsoft 365. You can find a version of this tip for the older menu interface of Word here: Changing What Is Pasted in a Dialog Box.
Do More in Less Time! An easy-to-understand guide to the more advanced features available in the Microsoft 365 version of Word. Enhance the quality of your documents and boost productivity in any field with this in-depth resource. Complete your Word-related tasks more efficiently as you unlock lesser-known tools and learn to quickly access the features you need. Check out Microsoft 365 Word For Professionals For Dummies today!
When working with variables in a macro, you may need to know the upper boundary dimension for an array. This can be ...
Discover MoreIf you are applying formatting from within a macro, you may want to change the alignment of various paragraphs. Here's ...
Discover MoreIf you save your macros in your Normal template, they'll be available whenever you are using Word. What if you don't want ...
Discover MoreFREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."
There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)
Got a version of Word that uses the ribbon interface (Word 2007 or later)? This site is for you! If you use an earlier version of Word, visit our WordTips site focusing on the menu interface.
Visit the WordTips channel on YouTube
FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2026 Sharon Parq Associates, Inc.
Comments