Dragon Drop - A Visual Basic Software Consultancy

Word Tips

Clearing The Clipboard

This was a strange assignment. Someone wanted to know how to empty the clipboard. On the face of this this was going to be straightforward. After all, couldn't we just copy the text of the Selection object to the clipboard?

Then after two or three thoughts there is the consideration that there may not be any documents open so there is no Selection object and, most certainly, no Range object. So, really something else had to be considered.

A quick scout on the help file produced the following code. Open up a new document a template and then add it in. All well and good until one decides to compile this code when it will fail on the declaration statement. The explanation is below.

Sub ClearClipboard()
  Dim oDataObject As DataObject

  Set oDataObject = New DataObject
  oDataObject.SetText ""
  Set oDataObject = Nothing
End Sub

Looking at the Help files it appears that the DataObject is associated with a user form. So, simply after adding a form to the template the code compiles. Note that the form was just added to the template project; it wasn't referenced in any way, it wasn't called in any way and, apart from just sitting there in the code, it does nothing. Well, apart from making the code compile -- and work.

What's the reason for this? Simple, the DataObject object is referenced in the MS Forms object. So all the programmer has to do is to add (and then remove a form if he wishes) as this will force the reference to the object or he can just simply add the MS Forms reference himself.

This template, if placed into the user's start-up folder, shows its presence by displaying a single button on a toolbar. Clicking on this simply empties the Clipboard by the simple process of copying a zero length string to it.

This code can be downloaded from here.


If there are any suggestions for updates then please drop me a mail at malcolm.smith@dragondrop.com.