使用GetText从剪贴板获取文本 - 避免空剪贴板上的错误

时间:2021-07-11 23:25:35

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找到空剪贴板时抛出错误。是否有任何类型的测试可以进一步应用于上游以避免尝试处理空剪贴板?

3 个解决方案

#1


16  

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

#2


5  

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对象库”库缺失/未激活。

Got it to work with this (http://excel-macro.tutorialhorizon.com/vba-excel-reference-libraries-in-excel-workbook/):

得到了它(http://excel-macro.tutorialhorizo​​n.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 Object Library“,那么你需要从system32浏览FM20.DLL文件”

#3


0  

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用于debuging的破解线......在测试之后错误消失了我... wierd但它以某种方式工作(Excel 2010)

myString = DataObj.GetText(1)
MsgBox myString

#1


16  

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

#2


5  

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对象库”库缺失/未激活。

Got it to work with this (http://excel-macro.tutorialhorizon.com/vba-excel-reference-libraries-in-excel-workbook/):

得到了它(http://excel-macro.tutorialhorizo​​n.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 Object Library“,那么你需要从system32浏览FM20.DLL文件”

#3


0  

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用于debuging的破解线......在测试之后错误消失了我... wierd但它以某种方式工作(Excel 2010)

myString = DataObj.GetText(1)
MsgBox myString