To empty the clipboard using VBA, the most reliable method is to use a Windows API call to the user32.dll library, specifically the OpenClipboard and EmptyClipboard functions. Because VBA does not have a native, single-line command to clear the system clipboard across all versions of Windows and Office, calling the operating system's own API ensures that the data is fully purged regardless of whether the clipboard contains text, images, or complex formatting.

What You'll Need

  • A Windows-based PC running Microsoft Excel, Word, or any Office application that supports VBA.
  • The Visual Basic Editor (VBE), which is accessed by pressing Alt + F11.
  • Administrative permissions to run macros (ensure macros are enabled in the Trust Center).
  • A basic understanding of how to insert a new Module into a VBA project.

Step-by-Step

  1. Open the Visual Basic Editor: Launch your Office application (e.g., Excel). Press Alt + F11 on your keyboard. This opens the VBE window where all your automation code is written and managed.
  2. Insert a New Module: In the top menu bar, click on Insert and then select Module. This creates a blank workspace where you can paste the API declarations and the subroutine. It is important to use a Module rather than placing the code inside a Sheet or ThisWorkbook object to ensure the functions are globally accessible within the project.
  3. Declare the Windows API Functions: Because you are interacting with the Windows operating system, you must tell VBA which external functions to use. Copy and paste the following declarations at the very top of the module.

    Note: For 64-bit versions of Office (which is standard for most modern installations), you must use the PtrSafe attribute.

    #If VBA7 Then
        Public Declare PtrSafe Function OpenClipboard Lib "user32" (ByVal hwnd As LongPtr) As Long
        Public Declare PtrSafe Function EmptyClipboard Lib "user32" () As Long
        Public Declare PtrSafe Function CloseClipboard Lib "user32" () As Long
    #Else
        Public Declare Function OpenClipboard Lib "user32" (ByVal hwnd As Long) As Long
        Public Declare Function EmptyClipboard Lib "user32" () As Long
        Public Declare Function CloseClipboard Lib "user32" () As Long
    #End If

    The #If VBA7 block ensures that the code remains compatible with both 32-bit and 64-bit versions of Office. OpenClipboard gains access to the clipboard, EmptyClipboard wipes the contents, and CloseClipboard releases the lock so other programs can use it again.

  4. Create the ClearClipboard Subroutine: Below the declarations, write the actual procedure that executes these steps in order. Copy and paste the following code:
    Sub ClearSystemClipboard()
        Dim result As Long
        
        ' Attempt to open the clipboard
        result = OpenClipboard(0)
        
        If result <> 0 Then
            ' If successful, empty the contents
            EmptyClipboard
            ' Always close the clipboard to prevent locking it for other apps
            CloseClipboard
        Else
            MsgBox "Unable to access the clipboard. It may be in use by another application.", vbExclamation
        End If
    End Sub

    The result variable checks if OpenClipboard returned a non-zero value. If it returns zero, it means another application (like a clipboard manager or a browser) has a lock on the clipboard, and the operation cannot proceed.

  5. Run the Code: You can test the code immediately. Copy some text from a website or document. Go back to the VBE, click anywhere inside the ClearSystemClipboard subroutine, and press F5. Now, try to paste (Ctrl + V) into any document; you will find that the clipboard is empty.
  6. Assign to a Button (Optional): To make this accessible for non-technical users, go back to your Excel sheet. Go to the Developer tab, click Insert, choose a Button (Form Control), and draw it on the sheet. When the "Assign Macro" window appears, select ClearSystemClipboard and click OK.

Common Mistakes

One of the most frequent errors is forgetting to call CloseClipboard. The clipboard is a shared system resource. When a program calls OpenClipboard, it places a lock on that resource. If your code crashes or finishes without calling CloseClipboard, other applications (including Windows itself) may be unable to copy or paste until the Office application is completely closed. This often manifests as a "Clipboard is busy" error in other software.

Another common mistake is neglecting the PtrSafe keyword in 64-bit Office environments. If you use code written for an older 32-bit version of Excel on a modern 64-bit machine, you will receive a "Compile error: The code in this project must be updated for use on 64-bit systems." Always use the conditional compilation block (#If VBA7) to ensure your tool works across different installations.

Some users attempt to clear the clipboard by simply assigning an empty string to a DataObject (using the MS Forms 2.0 library). While this works for text, it often fails to clear images, files, or formatted HTML content. The Windows API method described above is the only way to ensure a total purge of all data types.

Tips

To integrate this into a larger automation project, you can call the ClearSystemClipboard subroutine at the beginning of a macro that performs heavy copying and pasting. This ensures that no "ghost data" from a previous session interferes with your current process.

If you find that the clipboard is frequently locked by other apps, you can wrap the OpenClipboard call in a loop that retries the operation 3 to 5 times with a small delay (using Application.Wait) before giving up and showing the error message. This makes the macro more resilient in busy multitasking environments.

For those who want to avoid adding references to external libraries, the API method is superior because it does not require you to go to Tools > References and manually check a box for the Microsoft Forms 2.0 Object Library, which can be difficult to find in the list if no UserForm has been added to the project.

Troubleshooting

If the code does not work, first check if you have a clipboard manager installed (such as Ditto or the built-in Windows 10/11 Clipboard History). Some of these tools maintain their own history and may immediately refill the clipboard or block external attempts to empty it. Try disabling the clipboard history (Settings > System > Clipboard > Clipboard history Off) to see if the VBA code functions correctly.

If you encounter a "Compile Error," double-check that the API declarations are at the very top of the module. They cannot be placed inside a Sub or Function; they must be in the declarations section of the module.

If the macro runs without error but the clipboard still contains data, ensure you are not using a virtual machine or a remote desktop session where the clipboard is synced between two different operating systems. In some RDP configurations, the remote system may push the clipboard content back to the local system immediately after it is cleared.

Frequently Asked Questions

Will this delete my clipboard history in Windows 10/11?

No. The EmptyClipboard API call clears the current active item (the one that would be pasted with Ctrl+V). It does not purge the Windows Clipboard History (Win+V) list, as that is managed by a separate system service.

Do I need to add any special references in the Tools menu?

No. By using the Declare statement to call user32.dll, you are accessing the system files directly. No additional libraries or references are required for this specific method to work.

Can this code be used in Word or Access?

Yes. Since this code calls the Windows API rather than Excel-specific functions, it will work in any Microsoft Office application that supports VBA, including Word, Access, and PowerPoint.

Is it safe to run this code frequently?

Yes, it is safe. Clearing the clipboard is a standard system operation. However, ensure that CloseClipboard is always executed to avoid locking the resource for other applications.

Why not just use a DataObject to clear the clipboard?

The DataObject method only handles text. If the clipboard contains a picture, a file from File Explorer, or a formatted table, the DataObject approach will either fail or leave the non-text data intact. The API method is the only comprehensive solution.