如何检查剪贴板中是否没有文本? [英] How to check if clipboard is empty of text?

查看:124
本文介绍了如何检查剪贴板中是否没有文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我尝试从一个空的剪贴板粘贴,则会出现错误。我想在粘贴之前检查剪贴板中是否没有文本,以便避免这种情况。如何做到这一点?我知道可以通过错误处理来完成,但是我更喜欢避免错误的方法。

If I try to paste from an empty clipboard, I get an error. I would like to check if the clipboard is empty of text before pasting so that I can avoid this. How can this be accomplished? I'm aware it can be done through error handling, but I would prefer a method that avoids an error.

编辑-根据请求,添加创建错误的代码和错误消息:

Edit -- Per request, adding code that creates the error and the error message:

导致问题的代码:

Sub PasteFromEmptyClipBoard()

    Selection.Paste

End Sub

我收到的错误消息:

运行时错误'4605',此方法或属性不可用,因为剪贴板为空或无效。

"Run-time error '4605' This method or property is not available because the Clipboard is empty or is not valid."

推荐答案

非常重要 :在实施此代码之前,您必须先设置对 Microsoft Forms 2.0对象库的引用(如下面的附件屏幕截图所示)。当您滚动参考库时,您可能会发现它不是一个选项。要显示它,只需向项目添加一个表单(以后随时可以删除该表单)。

Very important: You must first set a reference to the "Microsoft Forms 2.0 Object Library" (as in the attached screenshot below) before implementing this code. You may find that it is not an option when you scroll through the reference libraries. To make it show up, just add a form to the project (you can always delete the form later).

Sub CheckClipboard()

        Dim myDataObject As DataObject

        Set myDataObject = New DataObject


        myDataObject.GetFromClipboard


        If myDataObject.GetFormat(1) = True Then 

            '''There is text on clipboard, so it's safe to paste

        Else 
            '''there is no text on the clipboard, so you may get error.

        End If

End Sub

这篇关于如何检查剪贴板中是否没有文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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