logo
search
VBA & Macro Problems

How to Insert a Hyperlink from Clipboard Using Word VBA Macro

Anum MushtaqAnum Mushtaq Oct 19, 2026 868 views

Question details

The user needs a VBA macro that reads the current URL stored in the clipboard and applies it as a hyperlink to the selected text, replacing the standard behavior where recorded macros reuse a static, hardcoded URL.

How to Insert a Hyperlink from the Clipboard Using a Word VBA Macro
Product
Word
Device & OS
not provided
Scenario
Automating the insertion of hyperlinks into a document where the target URL changes dynamically based on what is currently copied to the system clipboard.
Observed behavior
When recording a macro to insert a hyperlink, Word hardcodes the exact URL entered during the recording session. Running the macro repeatedly pastes the same original URL instead of fetching the current clipboard contents.
Before you start

Ensure you have the Developer tab enabled in your Word ribbon and are familiar with opening the VBA Editor using the Alt + F11 shortcut.

Solution 1Recommended

Create a Dynamic VBA Macro Using Microsoft Forms 2.0

Write a custom VBA script that utilizes the DataObject to fetch the current clipboard text and applies it as the hyperlink address.

The standard Word macro recorder only captures the final result of inserting a hyperlink, meaning it hardcodes the specific URL used during the recording. To make the macro dynamic, you must manually write the VBA code using the Microsoft Forms 2.0 Object Library to interact with your system's clipboard.

1
Open the VBA Editor

Press Alt + F11 on your keyboard to open the Microsoft Visual Basic for Applications (VBA) window.

2
Add the Required Object Library

In the VBA Editor, navigate to the top menu and click Tools > References. Scroll through the list, check the box next to 'Microsoft Forms 2.0 Object Library', and click OK. If it is not listed, you can browse for 'FM20.DLL' in your System32 folder.

3
Insert the Macro Code

Go to Insert > Module. In the new module window, paste the following code to retrieve the clipboard data and set it as the hyperlink address: Sub InsertLinkFromClipboard() Dim clipboardData As DataObject Set clipboardData = New DataObject clipboardData.GetFromClipboard ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, _ Address:=clipboardData.GetText(1), _ SubAddress:="", ScreenTip:="", _ TextToDisplay:=Selection.Range.Text End Sub

4
Run the Custom Macro

Return to your Word document. Select the text you want to hyperlink, copy your desired URL to the clipboard (Ctrl + C), and run the macro via the Developer tab or by pressing Alt + F8.

Create a Dynamic VBA Macro Using Microsoft Forms 2.0
Preserving Display Text: By setting TextToDisplay:=Selection.Range.Text in the macro, the original selected text in your document remains intact and visible, rather than being overwritten by the raw clipboard URL.
Free Microsoft Office alternative

Try WPS Office for Your Document Automation Needs

If you are looking for a reliable, highly compatible alternative to Microsoft Office, WPS Office offers a comprehensive suite for Word, Excel, and PowerPoint files. Enjoy a familiar interface and robust document processing capabilities without the expensive subscription fees.

  1. 1. Download and Install WPS Office: Visit the official WPS Office website and download the free installer for your operating system.
  2. 2. Open Your Documents: Launch WPS Writer and open your existing .docx files directly. Your formatting and text will remain perfectly intact.
  3. 3. Enjoy Powerful Features: Utilize the extensive built-in tools for document automation, formatting, and cross-platform syncing.
Free, lightweight, and fast-performing office suiteFully compatible with Microsoft Word .doc and .docx formatsFamiliar tabbed user interface for seamless migrationBuilt-in PDF editor and advanced document formatting tools
microsoft office alternative - wps office

Frequently Asked Questions

Why doesn't the Word macro recorder capture my clipboard paste action?

The macro recorder in Word captures the end result of an action rather than the exact steps taken in dialog boxes. When you paste a link into the Insert Hyperlink dialog while recording, Word records the specific URL you pasted, not the 'paste from clipboard' command itself.

How do I fix the 'User-defined type not defined' error when running this macro?

This error occurs because the DataObject requires the Microsoft Forms 2.0 Object Library. To resolve it, open the VBA Editor, go to Tools > References, and check the box for 'Microsoft Forms 2.0 Object Library'.

Can I bind this hyperlink macro to a keyboard shortcut for faster use?

Yes. In Word, go to File > Options > Customize Ribbon. Click the 'Customize' button next to Keyboard shortcuts. Under Categories, select 'Macros', find your InsertLinkFromClipboard macro, assign a new shortcut key (like Alt + H), and click Assign.