香港新浪網 MySinaBlog
« 上一篇 | 下一篇 »
Emily | 28th Jan 2009, 11:48 AM | General | (921 Reads)

測試 剪貼板上無內容

如果要判斷剪貼簿是否有資料可以使用 CountClipboardFormats API 函數
 

Declare Function CountClipboardFormats Lib "user32" () As Long

Function IsClipboardEmpty() As Boolean
If CountClipboardFormats = 0 Then
  IsClipboardEmpty = True
Else

  IsClipboardEmpty = False
End If
End Function

 

亦可以使用 MSForms library 的 DataObject , 要先設定 Microsoft Forms 2.0 Object Library

Sub Is_Clipboard_Empty()
     'Set reference to the Microsoft Forms 2.0 Object Library
    Dim sText As String
    Dim DataObj As New DataObject
    
    DataObj.GetFromClipboard
    On Error Resume Next
     ' Get clipboard
    sText = DataObj.GetText
    
     ' Clean non-printable characters
    sText = Application.WorksheetFunction.Clean(sText)
    
     ' Check if error
    If Err.Number <> 0 Then
         MsgBox "Clip board empty"
        On Error GoTo 0
        Exit Sub
         ' Check if zero length (blank)
        
    ElseIf Len(sText) = 0 Then
        MsgBox "Clip board blank"
        On Error GoTo 0
        Exit Sub
    End If
End Sub
 

 

Accessing The Windows Clipboard  : http://www.cpearson.com/excel/Clipboard.aspx