5

我正在使用一个函数从访问数据库中复制一堆图片并将它们存储到磁盘上。但是,此功能使用办公室剪贴板,剪贴板在大约 150 条记录后填满并导致程序崩溃。这是我清除剪贴板的方法

Private Declare Function apiOpenClipboard Lib "user32" Alias "OpenClipboard" (ByVal hwnd As Long) As Long
Private Declare Function apiEmptyClipboard Lib "user32" Alias "EmptyClipboard" () As Long
Private Declare Function apiCloseClipboard Lib "user32" Alias "CloseClipboard" () As Long

Sub EmptyClipboard()
    Call apiOpenClipboard(0&)
    Call apiEmptyClipboard
    Call apiCloseClipboard
End Sub

任何人都知道如何更有效地清除剪贴板

4

3 回答 3

1

您正在使用的功能是指 windows 剪贴板。这与Office 剪贴板不同

我发现清除剪贴板的唯一代码是Application.CommandBars("Clipboard").Controls(4).Execute,但是由于我禁用了办公室剪贴板(并且显然无法启用它),我看不出这是否是实际的解决方案

于 2013-01-21T14:47:59.937 回答
0

我在其他地方读到这将满足您的需求:

Option Explicit 

Public Sub ClearClipBoard() 
    Dim oData   As New DataObject 'object to use the clipboard

    oData.SetText Text:=Empty 'Clear
    oData.PutInClipboard 'take in the clipboard to empty it
End Sub 
于 2015-02-27T10:33:29.143 回答
0

试试:(旧线程,我知道;-))

    #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 ClearOfficeClipBoard()
    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
于 2017-08-30T11:48:10.210 回答