如何使用 VBA 清除 Office 剪贴板
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32736915/
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
How to Clear Office Clipboard with VBA
提问by Jean-Pierre Oosthuizen
How would you clear the Microsoft Office Clipboard using VBA, specifically Word VBA?
您将如何使用 VBA,特别是 Word VBA 清除 Microsoft Office 剪贴板?
I am copying a lot of data at time into the clipboard and don't want excessive data kept in the Clipboard.
我一次将大量数据复制到剪贴板中,并且不希望剪贴板中保留过多数据。
回答by jtchase08
Would a simple
一个简单的
Application.CutCopyMode = False
work for your situation, or is this option not viable?
适合您的情况,还是此选项不可行?
回答by Jean-Pierre Oosthuizen
Saw this on another post, and I have tested it with Word VBA.
在另一篇文章中看到了这个,我已经用 Word VBA 对其进行了测试。
'Clearing the Office Clipboard
Dim oData As New DataObject 'object to use the clipboard
oData.SetText text:=Empty 'Clear
oData.PutInClipboard 'take in the clipboard to empty it
Just copy and paste into your code where ever you need to clear the Clipboard.
只需将代码复制并粘贴到您需要清除剪贴板的位置即可。
Another thing I noticed is that when I .Quita program, say Excel, it keeps asking me if I want to keep the data is the Clipboard. A work around is to clear the clipboard using the above stated code. See below:
我注意到的另一件事是,当我.Quit编写程序时,例如 Excel,它不断询问我是否要保留剪贴板中的数据。解决方法是使用上述代码清除剪贴板。见下文:
'Clearing the Office Clipboard
Dim oData As New DataObject 'object to use the clipboard
oData.SetText text:=Empty 'Clear
oData.PutInClipboard 'take in the clipboard to empty it
'You can also just remove the Alert Messages from the Excel Program while
'the code is running
'Remove alert Messages from the Excel Program when closing
ExcelProgram.DisplayAlerts = False
'Quiting the Excel Application
ExcelProgram.Quit
I used the above example in a VBA code to import data from an Excel File. See here
我在 VBA 代码中使用了上面的示例来从 Excel 文件导入数据。看这里
回答by ChrisB
Here's a solution that worked for me. This is based on a post by by Zack Barresse on VBAexpress.com:
这是一个对我有用的解决方案。这是基于 Zack Barresse 在VBAexpress.com上的一篇文章:
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 ClearClipboard()
OpenClipboard (0&)
EmptyClipboard
CloseClipboard
End Sub
After copying this function to your VBA project, use ClearClipboardto clear it.
将此函数复制到您的 VBA 项目后,使用ClearClipboard清除它。
回答by NeepNeepNeep
This functionality is held within the library "Microsoft Forms 2.0 Object Library". To link to that library go to the VBA editor, then Tools, References and pick it out from the list if it's not already ticked.
此功能保存在库“Microsoft Forms 2.0 Object Library”中。要链接到该库,请转到 VBA 编辑器,然后转到“工具”、“参考”,如果尚未选中,请从列表中选择它。
You can do more funky stuff with a bunch of WinAPI calls, but I generally prefer avoiding those unless absolutely necessary.
你可以用一堆 WinAPI 调用做更多时髦的事情,但我通常更喜欢避免这些,除非绝对必要。
Also, don't forget about the DisplayAlerts property, which will suppress dialog boxes - although I'm not sure if it would always produce the desired result.
另外,不要忘记 DisplayAlerts 属性,它会抑制对话框 - 尽管我不确定它是否总是会产生所需的结果。

