2012-02-01 41 views

回答

0

那么,它需要一段时间,但这里是如何做到这一点。

为了重申问题,我想使用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 
+0

另一种方法:[检查剪贴板的文本](http://stackoverflow.com/questions/35595258/how-to-check-if-clipboard-is -empty-的文本) – Jon 2016-02-24 11:15:34

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 
+0

谢谢Siddharth,但只找到位图,我需要检测任何类型的图形对象,可以占用剪贴板上的图片插槽。 实际上,我想我离我原来的问题还有很远的地方,那就是在试图用GetText读取它之前,先检测剪贴板上是否有文本。查看ClipboardFormats属性的VBA帮助,如果不采用API的话,我们可以做些什么吗? – Roy 2012-02-02 20:04:39

+0

我感兴趣的唯一格式是xlClipboardFormatText。无论剪贴板上还有其他内容,如果该格式不可用,那么我想中止该过程。这需要在Error Trapping设置为Break on All Errors时抛出错误而完成。 我知道这里有一个答案,只是想出来:) – Roy 2012-02-02 20:05:03