无法使用 VBA 清除办公室剪贴板

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14440274/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 19:16:04  来源:igfitidea点击:

Can't clear office clipboard with VBA

vbams-accessaccess-vbams-access-2010

提问by DasPete

I am using a function to copy a bunch of pictures out of an access database and store them on to the disk. However, this function uses the office clipboard and the clipboard fills up after about 150 records and crashes the program. Here is how I am clearing the clipboard

我正在使用一个函数从访问数据库中复制一堆图片并将它们存储到磁盘上。但是,此功能使用办公室剪贴板,大约 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

Anyone know how to more effectively clear the clipboard

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

回答by SeanC

The functions you are using refer to the windows clipboard. This is different to the Office Clipboard

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

The only code I've found to clear that clipboard is Application.CommandBars("Clipboard").Controls(4).Execute, but as I have the office clipboard disabled (and, apparently, no way of enabling it), I can't see if that is the actual solution

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

回答by JV.

I have read elsewherethat this will do what you need:

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

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 

回答by EvR

Try: (old thread, I know ;-) )

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

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
  'by E v R  
 Sub ClearOfficeClipBoard()
 Dim Acc As Office.IAccessible

   With Application
        .CommandBars("Office Clipboard").Visible = True
   DoEvents
        Set Acc = .CommandBars("Office Clipboard").accChild(1)
        Set Acc = zetAcc(Acc, 3)
        Set Acc = zetAcc(Acc, 0)
        Set Acc = zetAcc(Acc, 3)
        Acc.accDoDefaultAction 2&
        .CommandBars("Office Clipboard").Visible = False
   End With

 End Sub
 Private Function zetAcc(myAcc As Office.IAccessible, myChildIndex As Long) As Office.IAccessible
 Dim ReturnAcc As Office.IAccessible
 Dim Count As Long, List() As Variant

    Count = myAcc.accChildCount
    ReDim List(Count - 1&)
    If AccessibleChildren(myAcc, 0&, ByVal Count, List(0), Count) = 0& Then Set zetAcc = List(myChildIndex)

 End Function