这是我在这里提出的问题的扩展:
Get text from clipboard using GetText - avoid error on empty clipboard
该问题的答案适用于避免使用空剪贴板的错误,但现在我发现我还必须处理仅包含图形而没有文本的剪贴板,并且此条件超过空剪贴板过滤器。
那么,当剪贴板上只有图形而没有文字时,如何中止该程序呢?
答案 0 :(得分:0)
您可以使用此代码测试剪贴板中数据的格式是否为图像。
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
答案 1 :(得分:0)
嗯,花了一段时间,但这是怎么做的。
为了重述这个问题,我想使用DataObject.GetFromClipboard从剪贴板中提取文本,并将错误陷阱设置为Break on All Errors,并且当剪贴板上没有找到文本时不会抛出错误。
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