这是我在这里提出的问题的扩展:
Get text from clipboard using GetText - avoid error on empty clipboard
这个问题的答案可以很好地避免剪贴板为空的错误,但是现在我发现我还必须处理一个仅包含图形而不包含文本的剪贴板,并且这种情况会超出剪贴板的空过滤条件。
因此,如果剪贴板上只有图形而没有文本,我该如何终止该过程?
最佳答案
您可以使用此代码测试剪贴板中数据的格式是否为图像。
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
关于error-handling - 使用GetText从剪贴板获取文本-仅在剪贴板上显示图形时避免错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9090269/