Please Note: This article is written for users of the following Microsoft Word versions: 2007, 2010, 2013, 2016, 2019, 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.

Changing What Is Pasted in a Dialog Box

Written by Allen Wyatt (last updated September 4, 2023)
This tip applies to Word 2007, 2010, 2013, 2016, 2019, and Word in Microsoft 365


1

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:

If you would like to know how to use the macros described on this page (or on any other page on the WordTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

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, 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.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Displaying Thumbnails and Full-Size Images

Sometimes images can be just too big to display in a document. Instead you may want to display a smaller, thumbnail-size ...

Discover More

A Fast Find-Next

Tired of the Find and Replace dialog box blocking the view of your worksheet when you are searching for information? Do ...

Discover More

Writing a Macro from Scratch

Recording macros is a great approach to getting started with macros, but at some point you'll need to create one from ...

Discover More

The First and Last Word on Word! Bestselling For Dummies author Dan Gookin puts his usual fun and friendly candor back to work to show you how to navigate Word 2013. Spend more time working and less time trying to figure it all out! Check out Word 2013 For Dummies today!

More WordTips (ribbon)

Determining Differences Between Dates

Do you need to do some simple math using dates in your macro? One of the easy functions you can use is the DateDiff function.

Discover More

Displaying the User Name in the Status Bar or Title Bar

Sometimes it can come in handy to know who the current computer user is, as far as Word is concerned. This tip presents ...

Discover More

Displaying a Message in the Status Bar

A great place for your macro to display status information is, well, in the status bar. Displaying the information is ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 2 + 5?

2019-01-24 09:14:21

Zak Khan

What i would like to do - is create a macro where I can change find the term and replace "NAME_FIRST" and replace it with the actual name of my client. Obviously each document had a different client name but the find NAME_FIRST remains the same throughout the client's document. So the client name can be found within the document in atable format, eg.

Student’s Name xxxxxxxx
Student’s Date of Birth xxxxxx
Customer Reference Number xxxxxxx
Student Contact Email xxxxxxxxx

So i would like to create a macro that will copy the student's name from the document and using the find and replace function have the student name replace the " NAME_FIRST" placeholder throughout the doument. Obviously, when i go a new doument, the student;'s name is different but the macro will paste the previous student name. Is there a way of changing this, so that it will paste the name of the student to for each new students document.


This Site

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.

Videos
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.