VBA Code to clear the Windows 10 64 bit clipboard

JeffGrant

Well-known Member
Joined
Apr 7, 2021
Messages
519
Office Version
  1. 365
Platform
  1. Windows
Hi All,

does anybody have vba code to clear the windows 10 64 bit clip board?

I stupidly continue to paste code from the clipboard that isn't correct and it mess up my formulas.

So I figure if I clear the clipboard at the end of the sub, then I cant mess it up :)

I would like to make my sheets a little bit more fool proof, or in this case Jeff-proof.

There are few references around, but the examples out there are not designed for windows 10 64 bit and so they code fails.

I am not talking about the vba clipboard because that can be cleared with application.cutcopymode = false. I am specifically wanting to clear the windows 10 clipboard.

Thanks in advance

Jeff.
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Cross-posting (posting the same question in more than one forum) is not against our rules, but the method of doing so is covered by #13 of the Forum Rules.

Be sure to follow & read the link at the end of the rule too!

Cross posted at: Macro to Clear win 10 64 bit clip board [SOLVED]
If you have posted the question at more places, please provide links to those as well.

If you do cross-post in the future and also provide links, then there shouldn’t be a problem.
 
Upvote 0
In the code in that linked thread you just need a couple of small changes to the declaration:

VBA Code:
#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
Public Sub ClearClipboard()
    OpenClipboard (0&)
    EmptyClipboard
    CloseClipboard
End Sub
 
Last edited:
Upvote 0
Ok Rory, I wasn't aware of that. Also, I set the thread as solved by mistake and thanked him publicly, and I didn't want to insult the person who help in the other forum by saying it worked and then it didn't.

I will be mindful of that in future.
 
Upvote 0
Hi Rory, I am using VBA in Excel 365. For whatever reason, I am still getting the Public Declare Statements in red which throws up a compile error.
 

Attachments

  • Capture.PNG
    Capture.PNG
    11.3 KB · Views: 87
Upvote 0
Sorry - brain fart on my part. I've updated the code above. The pit in the #Else part will still turn red but the code will work.
 
Upvote 0
No we are talking....bloody rippa...just did a bunch of tests and looks like I can my my code idiot-proof -> jeff-proof.......

thanks you kindly.......
 
Upvote 0
I think this code would be useful for me. Let me explain my situation and you let me know if this applies. In Outlook, I have a TextBox inside a UserForm. I have a CommandButton that when pressed is supposed to copy whatever is in that TextBox onto the clipboard so I can paste it on to Microsoft Teams. I have the following code for that:
VBA Code:
Private Sub CommandButton2_Click()
     Dim dataObj As MSForms.DataObject
    Set dataObj = New MSForms.DataObject
   
    With dataObj
        .SetText TextBox4.Text
        .PutInClipboard
    End With
End Sub

Sometimes this works, however, sometimes it doesn't. When I try to paste i get 2 squares instead. So I am assuming it's pasting some sort of special characters from the clipboard?
I have another CommandButton that when clicked clears the contents of the TextBox. Perhaps I can put the code here so that it clears the clipboard entirely so i won't be having the issue with the 2 squares?
 
Upvote 0
Hi Harvey.

This is what I use in VBA to clear the clipboard

VBA Code:
Sub ClearClipboard()
    OpenClipboard (0&)
    EmptyClipboard
    CloseClipboard
End Sub

cheers
 
Upvote 0

Forum statistics

Threads
1,216,101
Messages
6,128,843
Members
449,471
Latest member
lachbee

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