Issues with clearing the clipboard using VBA

RawlinsCross

Active Member
Joined
Sep 9, 2016
Messages
437
Found some code online that purports to clear the clipboard... but I'm getting a error I can't decipher. Wonder if anyone has a solution.

VBA Code:
'In a separate module

#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 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 = 0
#End If
Public Sub EvRClearOfficeClipBoard()
'On Error Resume Next
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")
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)) '<---- Fails here (see error snippit below)
Application.CommandBars("Office Clipboard").Visible = IsVis
With Application
    .DisplayClipboardWindow = True
End With
'On Error GoTo 0
End Sub

This is the error I'm getting :

1696355084688.png
 
You can trial this after the Call AltPrintScreen...
Code:
Dim T As Double
    T = Timer
    Do Until Timer - T > 1
      DoEvents
    Loop
Maybe some time is needed to copy to the clipboard before the paste (ie. the clipboard is still empty)? Dave
 
Upvote 1
Solution

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
So I've been using the "Timer" modifying code and I think it's working well. I'll keep an eye on it of course. Thanks for the tip - I'm sure this could be used in a variety of situations.
 
Upvote 0
You are welcome. Thanks for posting your outcome. Thanks also to Jon Peltier for the timer code. I've used it to resolve a number of mysterious problems. Have a nice day. Dave
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,177
Members
453,021
Latest member
Justyna P

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