cannot empty/clear the clipboard error

codesmith

Active Member
Joined
Apr 23, 2008
Messages
257
Hey all ....

I am using VBA in conjunction with an application called Quick Keys (basically software that allows you to program key strokes) ... and with this current project of mine ... I keep getting one of the following two errors:

"Cannot empty the clipboard" OR "Cannot clear the clipboard"


So I Googled, and based on the results it seems it is a VBA related error!


Does anyone know what may be going wrong here?
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
dmtcrainey... thanks for your reply, it was insightful.

I believe I have figured out why I get this error.

It seems when you copy large amounts of data repeatedly (x50+) (and say a standard screens worth of text) ... and you are constantly copying this amount... for some reason the memory management in xls windows gives up.

So as I suspected before reading this post, there must be some way of clearing the clipboard cache ... if this is possible.

Can anyone provide some function to clear the clipboard cache?

Thanks!
 
Upvote 0
"Cannot empty the clipboard" OR "Cannot clear the clipboard"

This is usually due to an application placing a lock on the clipboard.

Code:
Private Declare Function CloseClipboard Lib "user32" () As Long
Private Declare Function EmptyClipboard Lib "user32" () As Long
Private Declare Function OpenClipboard Lib "user32" (ByVal hwnd As Long) As Long

Sub Example()

    If OpenClipboard(Application.hwnd) = 0 Then
        'clipboard is locked
    Else
        EmptyClipboard
        CloseClipboard
    End If

End Sub
 
Upvote 0
"Cannot empty the clipboard" OR "Cannot clear the clipboard"

This is usually due to an application placing a lock on the clipboard.

Code:
Private Declare Function CloseClipboard Lib "user32" () As Long
Private Declare Function EmptyClipboard Lib "user32" () As Long
Private Declare Function OpenClipboard Lib "user32" (ByVal hwnd As Long) As Long
 
Sub Example()
 
    If OpenClipboard(Application.hwnd) = 0 Then
        'clipboard is locked
    Else
        EmptyClipboard
        CloseClipboard
    End If
 
End Sub


is if the clipboard is locked, dont do anything. but if it isnt empty and close?

I dont get how it will help me?!
 
Upvote 0
Hi,

I might be wrong, but thought that this would always work.
Code:
Option Explicit
 
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
 
Public Function ClearClipboard()
    OpenClipboard (0&)
    EmptyClipboard
    CloseClipboard
End Function
kind regards,
Erik
 
Upvote 0
"Can anyone provide some function to clear the clipboard cache?"

That is precisely what I did.

Code:
Sub Example()
 
    If OpenClipboard(Application.hwnd) = 0 Then
        'clipboard is locked
    Else
        EmptyClipboard
        CloseClipboard
    End If
 
End Sub

You cannot empty the clipboard if you cannot access it. If the function returns zero, the OpenClipboard function failed and there is no need to try and empty and then close it.

Hi Eric. Our code is identical except mine tests for successful access to the clipboard. Passing the Hwnd is not really neccesary when using this from VBA. It will still be associated with the Excel task...
 
Upvote 0
Alternatively:
Code:
Private Sub setClipboard()
  'Reference: Microsoft Forms 2.0 Object
    Dim MyData As DataObject
     
    On Error Resume Next
    Set MyData = New DataObject
    MyData.SetText strClip
    MyData.PutInClipboard
     
End Sub
Sub ClipboardClear()
  strClip = vbNull
  setClipboard
End Sub
 
Upvote 0
Thanks for the welcome. Aka tstom, right_click. I am tired of keeping track of all the usernames, so I just used my real name. :)
 
Upvote 0

Forum statistics

Threads
1,213,506
Messages
6,114,024
Members
448,543
Latest member
MartinLarkin

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top