Obtenga texto del portapapeles usando GetText: evite errores cuando solo tenga gráficos en el portapapeles

This is an extension to the question I asked here:

Obtenga texto del portapapeles usando GetText: evite errores en el portapapeles vacío

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?

preguntado el 01 de febrero de 12 a las 03:02

2 Respuestas

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

Respondido 02 Feb 12, 07:02

Thanks Siddharth, but that only finds bitmaps, and I would need to detect any kind of graphic object that can take up the picture slot on the clipboard. Actually, I think I'm getting rather far afield from my original problem which was just detecting whether or not there is text on the clipboard before trying to read it with GetText. Looking at VBA Help for ClipboardFormats Property, could we not do something with that without resorting to API's? - Roy

The only format I'm interested in is xlClipboardFormatText. No matter what else is on the clipboard, if that format is not available then I want to abort the procedure. And this needs to be done without throwing an error while Error Trapping is set to Break on All Errors. I know there's an answer in here just trying to get out :) - Roy

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

respondido 25 mar '12, 02:03

Otro método: Check Clipboard For Text - Jon

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.