Clear clipboard in excel vba

YasserKhalil
PlatinumLounger
Posts: 4961
Joined: 31 Aug 2016, 09:02

Clear clipboard in excel vba

Post by YasserKhalil »

Hello everyone
I have tried different solutions to clear the clipboard but I need to clear all that exists in clipboard section
oopp.png
I need VBA code to do that task
You do not have the required permissions to view the files attached to this post.

User avatar
HansV
Administrator
Posts: 79321
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Clear clipboard in excel vba

Post by HansV »

Do you really need to clear the entire Office Clipboard?

Application.CutCopyMode = False

will clear Excel's clipboard, but not the multi-item Office Clipboard.
Best wishes,
Hans

YasserKhalil
PlatinumLounger
Posts: 4961
Joined: 31 Aug 2016, 09:02

Re: Clear clipboard in excel vba

Post by YasserKhalil »

Yes I need to clear the entire Office Clipboard

User avatar
HansV
Administrator
Posts: 79321
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Clear clipboard in excel vba

Post by HansV »

I tried several methods mentioned in a Google search (using the MSForms DataObject and involving Windows APIs). The only one that worked for me in Microsoft 365 is the following (source: Clearing the clipboard in Office 365):

Code: Select all

Private Declare PtrSafe Function AccessibleChildren Lib "oleacc" (ByVal paccContainer As Office.IAccessible, _
    ByVal iChildStart As Long, ByVal cChildren As Long, _
    ByRef rgvarChildren As Any, ByRef pcObtained As Long) As Long

Sub EvRClearOfficeClipBoard()
    Dim cmnB, IsVis As Boolean, j As Long
    Set cmnB = Application.CommandBars("Office Clipboard")

   With Application
        .DisplayClipboardWindow = True
    End With

    IsVis = cmnB.Visible

    If Not IsVis Then
        cmnB.Visible = True
        DoEvents
    End If

    For j = 1 To 7
        AccessibleChildren cmnB, Choose(j, 0, 3, 0, 3, 0, 3, 1), 1, cmnB, 1
    Next

    cmnB.accDoDefaultAction CLng(0)

    Application.CommandBars("Office Clipboard").Visible = IsVis

    With Application
        .DisplayClipboardWindow = False
    End With
End Sub
Don't ask me to explain the code!
Best wishes,
Hans

YasserKhalil
PlatinumLounger
Posts: 4961
Joined: 31 Aug 2016, 09:02

Re: Clear clipboard in excel vba

Post by YasserKhalil »

Amazing. It worked for me too. I already tried several solutions too but nothing worked. This only worked for me
Thank you very much

PJ_in_FL
5StarLounger
Posts: 1139
Joined: 21 Jan 2011, 16:51
Location: Florida

Re: Clear clipboard in excel vba

Post by PJ_in_FL »

HansV wrote:
20 Jul 2024, 16:09
I tried several methods mentioned in a Google search (using the MSForms DataObject and involving Windows APIs). The only one that worked for me in Microsoft 365 is the following (source: Clearing the clipboard in Office 365):

...


Don't ask me to explain the code!
YIKES!
PJ in (usually sunny) FL

User avatar
HansV
Administrator
Posts: 79321
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Clear clipboard in excel vba

Post by HansV »

Weird code, isn't it?
Best wishes,
Hans

User avatar
SpeakEasy
5StarLounger
Posts: 608
Joined: 27 Jun 2021, 10:46

Re: Clear clipboard in excel vba

Post by SpeakEasy »

The way it works is by using the fact that a CommandBar implements the Accessibility interface (iAccessible), and we can use that to navigate the GUI hierarchy; a bit like tabbing around a userform

The code navigates it's way down through the hierarchy starting at the root msoCommandBar (and down through to the msoWorkPane, I believe) to the 'Clear All' control And then presses that button (note that the code as written will error if there is nothing in the Office Clipboard)

Given the code uses magic numbers, the question is how did the programmer know what numbers to use? I'd guess either dogged experimentation or, more likely, using a tool like Microsoft's Accessibility Insights for Windows to walk the tree.

User avatar
HansV
Administrator
Posts: 79321
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Clear clipboard in excel vba

Post by HansV »

Thanks for the insight!
Best wishes,
Hans