2012-02-01 53 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