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"
     


Click to view Terms of Use page

Click to view Disclaimer page