vba 使用 GetText 从剪贴板获取文本 - 避免在空剪贴板上出错
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9022245/
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
Get text from clipboard using GetText - avoid error on empty clipboard
提问by Roy
I'm using code like this to get text from off the Clipboard.
我正在使用这样的代码从剪贴板中获取文本。
Dim DataObj As New MSForms.DataObject
DataObj.GetFromClipboard
myString = DataObj.GetText
I use error handling to get the past the case where the Clipboard is empty, and everything is fine as long as I keep Error Trapping set to Break on Unhandled Errors.
我使用错误处理来解决剪贴板为空的情况,只要我将错误捕获设置为中断未处理的错误,一切都很好。
However, for unrelated reasons I want to set Error Trapping to Break on All Errors, and this throws an error at DataObj.GetText
when it finds the empty Clipboard. Is there any kind of test I can apply further upstream to avoid trying to process an empty Clipboard?
但是,由于不相关的原因,我想将错误捕获设置为在所有错误上中断,这会在DataObj.GetText
找到空剪贴板时引发错误。是否有任何类型的测试可以进一步应用于上游以避免尝试处理空剪贴板?
回答by Siddharth Rout
Does this help?
这有帮助吗?
Sub GetClipBoardText()
Dim DataObj As MSForms.DataObject
Set DataObj = New MsForms.DataObject '<~~ Amended as per jp's suggestion
On Error GoTo Whoa
'~~> Get data from the clipboard.
DataObj.GetFromClipboard
'~~> Get clipboard contents
myString = DataObj.GetText(1)
MsgBox myString
Exit Sub
Whoa:
If Err <> 0 Then MsgBox "Data on clipboard is not text or is empty"
End Sub
You will notice that it will handle an empty clipboard as well.
你会注意到它也会处理一个空的剪贴板。
You can empty the clipboard before testing the above code by using the code below. Please paste it in a module.
您可以在使用以下代码测试上述代码之前清空剪贴板。请将其粘贴到模块中。
Private Declare Function OpenClipboard Lib "User32.dll" _
(ByVal hWndNewOwner As Long) As Long
Private Declare Function EmptyClipboard Lib "User32.dll" () As Long
Private Declare Function CloseClipboard Lib "User32.dll" () As Long
Public Sub ClearClipboard()
Dim Ret
Ret = OpenClipboard(0&)
If Ret <> 0 Then Ret = EmptyClipboard
CloseClipboard
End Sub
EDIT: New Request by OP
编辑:OP 的新请求
Private Declare Function CountClipboardFormats Lib "user32" () As Long
Sub Sample()
If (CountClipboardFormats() = 0) = True Then
MsgBox "Clipboard is empty"
Else
MsgBox "Clipboard is not empty"
End If
End Sub
回答by Leon Wurr
Hope this helps someone else:
希望这对其他人有帮助:
I was getting the error "User-Defined type not defined" on the code posted by siddharth-rout
我在 siddharth-rout 发布的代码上收到错误“未定义用户定义的类型”
Turns out the “Microsoft Forms 2.0 Object Library” library was missing/not activated.
结果发现“Microsoft Forms 2.0 Object Library”库丢失/未激活。
Got it to work with this (http://excel-macro.tutorialhorizon.com/vba-excel-reference-libraries-in-excel-workbook/):
让它与这个一起工作(http://excel-macro.tutorialhorizon.com/vba-excel-reference-libraries-in-excel-workbook/):
"Some-times you won't find the desired ref-er-ences in the list, say you won't find “Microsoft Forms 2.0 Object Library” in the tool/reference list in that case you need to browse the FM20.DLL file from the system32"
“有时您不会在列表中找到所需的引用,比如在工具/引用列表中找不到“Microsoft Forms 2.0 对象库”,在这种情况下,您需要浏览 FM20.DLL来自系统的文件 32"
回答by agadir
add the follwoing code just b4 the breaking line for debuging.... the error disapeared for me after that test.. wierd but it somehow works (Excel 2010)
添加以下代码只是 b4 用于调试的断线.... 测试后错误消失了.. 奇怪但它以某种方式工作(Excel 2010)
myString = DataObj.GetText(1)
MsgBox myString