'How to clear contents of clipboard?

In Excel sheet Products, in a table named Products I've example data.

I want to copy the three leftmost columns to the clipboard, next save it's contents to file, read first line from this file, copy this to the clipboard and paste to cells of another sheet Results.

Dim DataObj As DataObject
Set DataObj = GetObject("New:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}") 'or simply New DataObject
Dim strOut As String, strIn As String
With Sheets("Products").ListObjects("Products")
    Union(.ListColumns(1).DataBodyRange, .ListColumns(2).DataBodyRange, .ListColumns(3).DataBodyRange).Copy 'data copied to Clipboard
End With
DataObj.GetFromClipboard
strOut = DataObj.GetText 'data copied from Clipboard to string variable
DataObj.SetText Text:=Empty
DataObj.PutInClipboard 'Clipboard clearing #1
Application.CutCopyMode = False 'Clipboard clearing #2
ClearClipboard 'Clipboard clearing #3
Open ThisWorkbook.Path & "\products.csv" For Output As #1
Print #1, strOut;
Close #1 'data saved
Open ThisWorkbook.Path & "\products.csv" For Input As #1
Line Input #1, strIn
Close #1 'data loaded
DataObj.SetText strIn
DataObj.PutInClipboard 'data copied from string variable to Clipboard
Sheets("Results").Range("A1").PasteSpecial Paste:=xlPasteValues 'data should be pasted to single row
Application.CutCopyMode = False

Yet known ClearClipboard code:

Public Declare PtrSafe Function OpenClipboard Lib "user32" (ByVal hwnd As Long) As LongPtr
Public Declare PtrSafe Function EmptyClipboard Lib "user32" () As LongPtr
Public Declare PtrSafe Function CloseClipboard Lib "user32" () As LongPtr

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

After populating the strOut variable I applied three methods of clipboard clearing to be sure it's empty. Because of loading only the first line from file to the strIn variable and pasting it to cells thru Paste or PasteSpecial method, I expect to see one row of data, meanwhile there are all the rows I had before clearing the clipboard.



Solution 1:[1]

You're not clearing the office clipboard (in which these 3 lines are copied)

for clearing the office clipboard:

#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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 EvR