使用 GetText 从剪贴板获取文本 - 当剪贴板上只有图形时避免错误

发布于 2025-01-01 01:25:35 字数 305 浏览 1 评论 0原文

这是我在这里提出的问题的扩展:

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

该问题的答案很好地避免了空剪贴板的错误,但现在我发现我还必须处理仅包含图形而不包含任何内容的剪贴板文本,并且此条件通过了空剪贴板过滤器。

那么,当剪贴板上只有图形而没有文本时,如何中止该过程呢?

This is an extension to the question I asked here:

Get text from clipboard using GetText - avoid error on empty clipboard

The answer to that question worked fine for avoiding errors with an empty clipboard, but now I find I also have to handle a clipboard that contains only a graphic and no text, and this condition gets past the empty clipboard filter.

So, how can I abort the procedure when there's only a graphic and no text on the clipboard?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

空‖城人不在 2025-01-08 01:25:35

您可以使用此代码测试剪贴板中的数据格式是否为图像。

Option Explicit

Private Declare Function OpenClipboard Lib "user32" _
(ByVal hwnd As Long) As Long

Private Declare Function GetClipboardData Lib "user32" _
(ByVal wFormat As Integer) As Long

Private Declare Function CloseClipboard Lib "user32" () As Long

Const CF_BITMAP = 2

Sub Sample()
    Dim RetClpB As Long
    Dim RetBmp As Long

    '~~> Open Clipboard
    RetClpB = OpenClipboard(0&)

    '~~> Check if we were successful
    If RetClpB <> 0 Then
        '~~> Test if the data in Clipboard is an image by
        '~~> trying to get a handle to the Bitmap
        RetBmp = GetClipboardData(CF_BITMAP)

        '~~> If found
        If RetBmp <> 0 Then
            MsgBox "data in clipboad is an image"
        Else
            MsgBox "data in clipboad is not an image"
        End If
    End If

    '~~> Close Clipboard
    RetClpB = CloseClipboard
End Sub

You can test if format of the data in the Clipboard is an image or not by using this code.

Option Explicit

Private Declare Function OpenClipboard Lib "user32" _
(ByVal hwnd As Long) As Long

Private Declare Function GetClipboardData Lib "user32" _
(ByVal wFormat As Integer) As Long

Private Declare Function CloseClipboard Lib "user32" () As Long

Const CF_BITMAP = 2

Sub Sample()
    Dim RetClpB As Long
    Dim RetBmp As Long

    '~~> Open Clipboard
    RetClpB = OpenClipboard(0&)

    '~~> Check if we were successful
    If RetClpB <> 0 Then
        '~~> Test if the data in Clipboard is an image by
        '~~> trying to get a handle to the Bitmap
        RetBmp = GetClipboardData(CF_BITMAP)

        '~~> If found
        If RetBmp <> 0 Then
            MsgBox "data in clipboad is an image"
        Else
            MsgBox "data in clipboad is not an image"
        End If
    End If

    '~~> Close Clipboard
    RetClpB = CloseClipboard
End Sub
固执像三岁 2025-01-08 01:25:35

好吧,这花了一些时间,但这里是如何做到这一点的。

只是为了重述问题,我想使用 DataObject.GetFromClipboard 从剪贴板中提取文本,并将错误捕获设置为“在所有错误上中断”,并且当剪贴板上没有找到文本时不会抛出错误。

 Sub TEST_getClipText()
    Debug.Print getClipText
 End Sub
 Function getClipText() As String
    Dim DataObj As MsForms.DataObject
    Set DataObj = New MsForms.DataObject 'tnx jp
    Dim V As Variant
    For Each V In Application.ClipboardFormats
       If V = xlClipboardFormatText Then
          DataObj.GetFromClipboard
          getClipText = DataObj.getText(1)
          Exit Function
       End If
    Next V
    MsgBox "No text on clipboard"
 End Function

Well, it took a while, but here's how to do it.

Just to restate the problem, I want to extract text from the clipboard using DataObject.GetFromClipboard, with error trapping set to Break on All Errors, and without throwing an error when there's no text found on the clipboard.

 Sub TEST_getClipText()
    Debug.Print getClipText
 End Sub
 Function getClipText() As String
    Dim DataObj As MsForms.DataObject
    Set DataObj = New MsForms.DataObject 'tnx jp
    Dim V As Variant
    For Each V In Application.ClipboardFormats
       If V = xlClipboardFormatText Then
          DataObj.GetFromClipboard
          getClipText = DataObj.getText(1)
          Exit Function
       End If
    Next V
    MsgBox "No text on clipboard"
 End Function
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文