logo
search
VBA & Macro Problems

How to Create a Word VBA Macro to Replace Text and Copy to Clipboard

Maira MehtabMaira Mehtab Sep 22, 2026 869 views

Question details

The user wants to create a Word VBA macro to automatically paste clipboard content as plain text, replace specific characters such as angle brackets (<, >) and hash signs (#), and then copy the processed text back to the clipboard.

Product
Word
Device & OS
not provided
Scenario
Automating text formatting and bulk character replacement in a document using VBA without manual Find and Replace operations.
Observed behavior
The goal is to streamline the text processing workflow by manipulating clipboard data directly through a macro.
Before you start

Before creating or running macros, ensure that the Developer tab is enabled in your Word ribbon and that you have enabled macros in the Trust Center settings for your document.

Solution 1Recommended

Create a VBA Macro to Process and Copy Text

Use a custom VBA script to paste unformatted text into a temporary range, perform a find and replace for specific characters, and copy the final text back to the clipboard.

This solution uses Microsoft Word's built-in Visual Basic for Applications (VBA) to automate clipboard manipulation. By pasting the text first, we can leverage Word's robust Find and Replace features before pushing the data back to the clipboard.

1
Enable the Developer Tab

Go to File > Options > Customize Ribbon. In the right pane, check the box next to 'Developer' and click OK to display the Developer tab on your ribbon.

2
Add Necessary Object References

Press Alt + F11 to open the VBA Editor. Go to Tools > References, scroll down, and check 'Microsoft Forms 2.0 Object Library'. This is required to interact with the clipboard via VBA.

3
Insert a New Module

In the VBA Editor, click Insert > Module. This will open a blank window where you can write your macro code.

4
Write the Macro Code

Write a script that creates a DataObject, uses `Selection.PasteSpecial DataType:=wdPasteText` to paste unformatted text, applies `Selection.Find.Execute` to replace '<', '>', and '#' with hyphens, and finally copies the resulting text.

5
Run the Macro

Close the VBA Editor. In your Word document, press Alt + F8, select your newly created macro from the list, and click Run.

Macro Security Warning: Avoid running macros from untrusted documents or unknown sources to protect your system from potentially malicious scripts.

Automate Text Formatting with WPS Office

WPS Office provides robust built-in support for VBA macros, allowing you to easily automate repetitive tasks like pasting, replacing characters, and copying text directly within your documents.

  1. 1. Install WPS Office: Download and install WPS Office, then open WPS Writer.
  2. 2. Open the VBA Editor: Navigate to the Developer tab on the ribbon and click on the 'Visual Basic' icon.
  3. 3. Insert Your Code: Click Insert > Module, paste your text processing macro, and close the editor.
  4. 4. Execute the Macro: Click 'Macros' in the Developer tab, select your script, and click Run to automate your clipboard task.
Full support for running and editing VBA macros and scripts.Highly compatible with Microsoft Word document formats (.docx, .docm).Lightweight installation with extremely fast loading speeds.Familiar interface making it easy to access the Developer tools.
microsoft office alternative - wps office

Frequently Asked Questions

Why am I getting a 'User-defined type not defined' error when using the clipboard in VBA?

This error typically occurs when the 'Microsoft Forms 2.0 Object Library' is not enabled. In the VBA editor, go to Tools > References and check this library to allow VBA to interact with the clipboard using the DataObject.

Can I process the clipboard text purely in the background without pasting it into the document?

Yes. By utilizing the MSForms.DataObject in VBA, you can retrieve text directly from the clipboard into a string variable, use standard VBA functions like Replace() to modify the characters, and push it back to the clipboard entirely in the background.

How do I save a macro so it is available in all my Word documents?

To make a macro globally available, save it in the Normal.dotm template rather than the specific document you are currently working on. You can select 'Normal' from the Project Explorer pane on the left side of the VBA Editor before inserting your module.