使用GetText从剪贴板获取文本-避免在空剪贴板上出错 [英] Get text from clipboard using GetText - avoid error on empty clipboard

查看:103
本文介绍了使用GetText从剪贴板获取文本-避免在空剪贴板上出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用这样的代码从剪贴板中获取文本。

 将DataObj用作新的MSForms.DataObject 
DataObj.GetFromClipboard
myString = DataObj.GetText

我使用错误处理来获取过去的情况是剪贴板为空,只要我将错误陷阱设置为在未处理的错误时中断,一切都很好。



但是,出于不相关的原因,我想要将错误陷阱设置为在所有错误上都中断,当它找到空剪贴板时,这会在 DataObj.GetText 处引发错误。我可以在上游进行任何类型的测试,以避免尝试处理空的剪贴板吗?

解决方案

使用出现错误时转到,如下所示:

  Sub GetClipBoardText()
Dim DataObj As MSForms.DataObject
设置DataObj = New MsForms.DataObject'< ~~根据jp的建议修改

出错时转到$ a

从剪贴板获取数据。
DataObj.GetFromClipboard

’~~>获取剪贴板内容
myString = DataObj.GetText(1)
MsgBox myString

Exit Sub
哇:
如果Err< 0然后,MsgBox剪贴板上的数据不是文本或为空。
结束子

您会注意到它也会处理一个空的剪贴板。


NB:要使代码正常工作,必须具有对 Microsoft Forms 2.0对象库的引用; (可以在32位计算机上的 C:\windows\system32\FM20.dll C:\中找到此文件64Windows\sysWOW64\FM​​20.dll (在64位计算机上),否则会出现错误未定义用户定义的类型。。 p>

您可以在使用以下代码测试上面的代码之前清空剪贴板。请粘贴到模块中。

 私有声明函数OpenClipboard Lib User32.dll _ 
(只要ByVal hWndNewOwner)

私有声明函数EmptyClipboard Lib User32.dll ()只要

私有声明函数CloseClipboard Lib User32.dll; ()As Long

Public Sub ClearClipboard()
Dim Ret

Ret = OpenClipboard(0&)
如果Ret< 0然后Ret = EmptyClipboard
CloseClipboard
End Sub

编辑:您还可以确定剪贴板是否通过使用以下代码为空:

 私有声明函数CountClipboardFormats Lib user32; ()只要Long 

Sub Sample()
If(CountClipboardFormats()= 0)= True,则
MsgBox剪贴板为空;
其他
MsgBox剪贴板不为空
结束如果
结束子


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?

解决方案

Handle the errors with On Error GoTo as shown here:

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.

NB: to make the code work, you must have a reference to "Microsoft Forms 2.0 Object Library" (this file can be found at C:\windows\system32\FM20.dll on 32-bit machines, or at C:\Windows\sysWOW64\FM20.dll on 64-bit machines), otherwise you'd get the error "User-Defined type not defined".

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: you may also determine if the clipboard is empty by using this code:

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

这篇关于使用GetText从剪贴板获取文本-避免在空剪贴板上出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆