Manipulating the clipboard using VBA

Article contributed by Jonathan West

Although VB6 has a Clipboard object which you can manipulate, Word VBA
doesn't. This is how to clear the clipboard in VBA:

Dim MyData As DataObject

Set MyData = New DataObject
MyData.SetText ""
MyData.PutInClipboard

This is how to get the text on the clipboard into a string variable:

Dim MyData As DataObject
Dim strClip As String

Set MyData = New DataObject
MyData.GetFromClipboard
strClip = MyData.GetText

This is how to get the text from a string variable into the clipboard:

Dim MyData As DataObject
Dim strClip As String
strClip = "Hi there"

Set MyData = New DataObject

MyData.SetText strClip 
MyData.PutInClipboard

The DataObject object is a part of the Forms library in VBA. In order to make this code work, you must do one of two things.

  1. Have at least one UserForm in your project, or
  2. In the VBA editor, go to Tools, References, and set a reference to the "Microsoft Forms 2.0 Object Library";
     

Troubleshooting – what to do if PutInClipboard puts two symbols in the Clipboard instead of the correct data

[Tip added by Lene Fredborg 29-Jan-2023]

You may experience that Word shows two symbols like the ones below when inserting the result of PutInClipboard:

You will find a fix for this issue in Lene Fredborg's article PutInClipboard stores two symbols instead of correct data – How to FIX the issue.