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?
 
I found this code:

Rich (BB code):
Function JR_ForceEmpty() 
    OpenClipboard 0& 
    EmptyClipboard 
End Function


And it has kind of helped, because the error I receive now is "cannot open clipboard".

Is there anyway to simplify the cut/copy such that when a new bit of string is copied to clipboard the previous ones are deleted?

It's really annoying me now!!
 
Upvote 0

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
You forgot to close it after you opened and emptied it. :)

Code:
Function JR_ForceEmpty() 
    OpenClipboard 0& 
    EmptyClipboard 
    CloseClipboard
End Function
 
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...


Sorry mate, I get it now. If there is nothing in the clipboard, then the clipboard doesn't exist! Makese much sense.

I haven't tried your solution yet ... but I will let you know how it goes if you are interested.
 
Upvote 0
Ok guys/girls ... in regards to this problem....

What is it referring to here? I am lead to believe the array of clipboard entries in MS Office is causing the problem. If I were copy/pasting from say notepad ... then I assume this problem would not occur, because it only has a single clipboard.

So now I am getting a "Cannot Empty Clipboard".

I am using code:

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



So the chronological list of errors I have encountered:


Cannot close the clipboard
Cannot open the clipboard *** this was fixed with last line of code above
And now Cannot empty the clipboard.


Can someone please help me out with this, it's really frustrating ...


Is there a way to switch the clipboard memory to single entry? so instead of an array of clipboard entries, have just one (like notepad)?


thanks all...
 
Upvote 0
Please descibe the steps taken to "lock" the clipboard. I found no problem using Excel's Office Clipboard and the methods posted here.
 
Upvote 0
Erik wrote:
erik.van.geit
Re: cannot empty/clear the clipboard error
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



I have been using this code for years, but it does not work in Office 64 bit. Is there something like it that works for both 64 and 32 bit? I have not tried all of the things found in the entire thread. And since it was dated back in 2008 I thought there might be a better way to handle it?
 
Upvote 0
You will need to use PtrSafe for 64bit.
Code:
    Declare PtrSafe Function CloseClipboard Lib "User32" () As Long
    Declare PtrSafe Function OpenClipboard Lib "User32" (ByVal hwnd As LongPtr) As LongPtr
    Declare PtrSafe Function EmptyClipboard Lib "User32" () As Long
 
Upvote 0
That works! Thanks

Any idea why it doesn't like this?
Code:
Public Function GetMonitoId()

It gives me "Type Mismatch" compile error.
 
Upvote 0
It would be best to start a new thread for that.

That command is in Java. There is a way to run a JavaScript in VBA. The better route is to state what you are trying to find out, the problem, rather than detailing a solution path.
 
Upvote 0
Thanks, I had thought it might be a related issue since it happened in the same workbook. Both happened in the 64 bit Office version.
 
Upvote 0

Forum statistics

Threads
1,215,365
Messages
6,124,512
Members
449,167
Latest member
jrob72684

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