0

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?

Community
  • 1
  • 1
Roy
  • 653
  • 2
  • 11
  • 20

2 Answers2

0

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
Siddharth Rout
  • 147,039
  • 17
  • 206
  • 250
  • 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 Feb 02 '12 at 20:04
  • 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 Feb 02 '12 at 20:05
0

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
Roy
  • 653
  • 2
  • 11
  • 20
  • Another method: [Check Clipboard For Text](http://stackoverflow.com/questions/35595258/how-to-check-if-clipboard-is-empty-of-text) – Jon Feb 24 '16 at 11:15