1
votes

I am using Office 365 under Windows 10, 64 bit. I am trying to clear the clipboard. The macro recorder produces an empty sub.

The following attempts are mostly collected from How to Clear Office Clipboard with VBA :

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 Sub ClearClipboardA()
    Application.CutCopyMode = False
End Sub

Public Sub ClearClipBoardB()
    ' Source: http://www.vbaexpress.com/kb/getarticle.php?kb_id=462
    Dim oData As New DataObject
    oData.SetText Text:=Empty ' Clear
    oData.PutInClipboard ' Putting empty text into the clipboard to empty it
End Sub

Public Sub ClearClipboardC()
    OpenClipboard (0&)
    EmptyClipboard
    CloseClipboard
End Sub

Version A:

Method or data member not found

Version B: Runs without clearing the clipboard. A small yellow status window appears shortly:

"7 of 24 - Clipboard | Element not fetched"

(translated into English)

Version C: Nothing seems to happen.

In the above reference user iamstrained writes: “... if anyone is hunting for how to do this in Office 365 under 64-Bit, you now need to use the modifications for backwards compatibility to make this work: Private Declare PtrSafe and LongPtr as your two changes to these values will resolve issues and allow it to still work.”

I found a reference to a Microsoft page, where this has perhaps been done:

https://docs.microsoft.com/da-dk/office/vba/access/Concepts/Windows-API/send-information-to-the-clipboard

Using subs shown here I can insert text into the clipboard and extract from it, but not clear it.

3
Does this answer your question? How to Clear Office Clipboard with VBAMartin

3 Answers

0
votes

I can confirm that the code below clears the Windows clipboard

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

Public Sub ClearClipboard()
   OpenClipboard (0&)
   EmptyClipboard
   CloseClipboard
End Sub

You can download the pointer safe declarations from https://www.microsoft.com/en-us/download/confirmation.aspx?id=9970

0
votes

to clear the office Clipboard (from Excel):

#If VBA7 Then
    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
    Public Const myVBA7 As Long = 1
#Else
    Private Declare 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
    Public Const myVBA7 As Long = 0
#End If

Public Sub EvRClearOfficeClipBoard()
    Dim cmnB, IsVis As Boolean, j As Long, Arr As Variant
    Arr = Array(4, 7, 2, 0)                      '4 and 2 for 32 bit, 7 and 0 for 64 bit
    Set cmnB = Application.CommandBars("Office Clipboard")
    IsVis = cmnB.Visible
    If Not IsVis Then
        cmnB.Visible = True
        DoEvents
    End If

    For j = 1 To Arr(0 + myVBA7)
        AccessibleChildren cmnB, Choose(j, 0, 3, 0, 3, 0, 3, 1), 1, cmnB, 1
    Next
        
    cmnB.accDoDefaultAction CLng(Arr(2 + myVBA7))

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

End Sub
-1
votes

I've used this above code snippet and it worked well until recent software updates that prevented me to clear the office clipboard without opening clipboard window. My solution it's very simple - add just this to the code:

#If VBA7 Then
    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
    Public Const myVBA7 As Long = 1
#Else
    Private Declare 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
    Public Const myVBA7 As Long = 0
#End If

Public Sub EvRClearOfficeClipBoard()
    Dim cmnB, IsVis As Boolean, j As Long, Arr As Variant
    Arr = Array(4, 7, 2, 0)                      '4 and 2 for 32 bit, 7 and 0 for 64 bit
    Set cmnB = Application.CommandBars("Office Clipboard")
   
'Just add here...First
'---------------------    
    With Application
       .DisplayClipboardWindow = True
    End With

    IsVis = cmnB.Visible 

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

    For j = 1 To Arr(0 + myVBA7)
        AccessibleChildren cmnB, Choose(j, 0, 3, 0, 3, 0, 3, 1), 1, cmnB, 1
    Next
        
    cmnB.accDoDefaultAction CLng(Arr(2 + myVBA7))

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

'And finish with this
'--------------------
    With Application
       .DisplayClipboardWindow = False
    End With

End Sub