How to clear the Clipboard when exiting Microsoft Word

This week a colleague approached me to consult about a problem he had encountered, where it was necessary to ensure that the Clipboard was empty when Microsoft Word exits. It was part of a legacy Word automation solution that he wasn’t in a position to change just yet, and it would crash if there was anything left on the clipboard.

Two approaches came to mind, one using a Word VBA Macro, the other using a VSTO add-in. Here is an attempt at a solution for the first approach – let’s hope it fixes the problem.

You can hook into events using Word VBA quite easily, either by creating a macro with a special name (reference: http://msdn.microsoft.com/en-us/library/bb208800.aspx), or by setting up event handlers. In this case, we will use a macro called AutoExit which is invoked “when you exit Word or unload a global template”. We’ll save the macro in a template file and have it loaded as a global template, so this event should be ideal for our purposes.

The first step is to load Word and get to the Visual Basic editor (press Alt-F11). Here, double-click on the ThisDocument entry in the project explorer to open up a code window:

Then, copy and paste the following code (which I will explain soon) into the code window you just opened:

'Events: http://msdn.microsoft.com/en-us/library/bb208800.aspx
Sub AutoExit()
    ClearClipboard
End Sub

Sub ClearClipboard()
    Dim MyData As Object
    ' source: http://akihitoyamashiro.com/en/VBA/LateBindingDataObject.htm
    Set MyData = CreateObject("new:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
    MyData.SetText ""
    MyData.PutInClipboard
    Set MyData = Nothing
End Sub

Now save the document as a Word Macro-Enabled Document Template (.dotm) with a name such as ClearClipboardOnWordExit.dotm:

Place this document in the Word Startup folder. By default, it should be %appdata%\Microsoft\Word\Startup, but you can find out where it really is by going to Word Options, Advanced, scrolling to the bottom and clicking on File Locations:

Word will load any templates in this folder as global templates when it starts.

That’s about it! Now to test it, open Word afresh, type something and copy it to the clipboard. Then exit Word and check to confirm that the clipboard no longer has the text you just copied.

Explanation of the code

  • The name of the macro, AutoExit, makes it get called when Word is closing:
    Sub AutoExit()
        ClearClipboard
    End Sub
    
  • Then we create a DataObject, which is part of the Microsoft Forms 2.0 Object Library. However, instead of adding a reference to it, I prefer using late binding so I found this page that describes how to do it using the clsid: http://akihitoyamashiro.com/en/VBA/LateBindingDataObject.htm

        Dim MyData As Object
        Set MyData = CreateObject("new:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
    
  • Using this object, we set a blank string to the clipboard, effectively clearing it:
        MyData.SetText ""
        MyData.PutInClipboard
    
This entry was posted in Technology and tagged , , , . Bookmark the permalink. Trackbacks are closed, but you can post a comment.

One Comment

  1. Posted 2014/01/27 at 8:40 am | Permalink

    very nice!!!!!!!!!!!!!!

Post a Comment

Your email is never published nor shared. Required fields are marked *

You may use these HTML tags and attributes <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*
*

Time limit is exhausted. Please reload the CAPTCHA.