23

I'm using code like this to get text from off the Clipboard.

Dim DataObj As New MSForms.DataObject
DataObj.GetFromClipboard
myString = DataObj.GetText

I use error handling to get the past the case where the Clipboard is empty, and everything is fine as long as I keep Error Trapping set to Break on Unhandled Errors.

However, for unrelated reasons I want to set Error Trapping to Break on All Errors, and this throws an error at DataObj.GetText when it finds the empty Clipboard. Is there any kind of test I can apply further upstream to avoid trying to process an empty Clipboard?

ashleedawg
  • 20,365
  • 9
  • 72
  • 105
Roy
  • 653
  • 2
  • 11
  • 20

2 Answers2

30

Handle the errors with On Error GoTo as shown here:

Sub GetClipBoardText()
   Dim DataObj As MSForms.DataObject
   Set DataObj = New MsForms.DataObject '<~~ Amended as per jp's suggestion

   On Error GoTo Whoa

   '~~> Get data from the clipboard.
   DataObj.GetFromClipboard

   '~~> Get clipboard contents
   myString = DataObj.GetText(1)
   MsgBox myString

   Exit Sub
Whoa:
   If Err <> 0 Then MsgBox "Data on clipboard is not text or is empty"
End Sub

You will notice that it will handle an empty clipboard as well.

NB: to make the code work, you must have a reference to "Microsoft Forms 2.0 Object Library" (this file can be found at C:\windows\system32\FM20.dll on 32-bit machines, or at C:\Windows\sysWOW64\FM20.dll on 64-bit machines), otherwise you'd get the error "User-Defined type not defined".

You can empty the clipboard before testing the above code by using the code below. Please paste it in a module.

Private Declare Function OpenClipboard Lib "User32.dll" _
(ByVal hWndNewOwner As Long) As Long
  
Private Declare Function EmptyClipboard Lib "User32.dll" () As Long
 
Private Declare Function CloseClipboard Lib "User32.dll" () As Long
 
Public Sub ClearClipboard()
    Dim Ret
  
    Ret = OpenClipboard(0&)
    If Ret <> 0 Then Ret = EmptyClipboard
    CloseClipboard
End Sub

EDIT: you may also determine if the clipboard is empty by using this code:

Private Declare Function CountClipboardFormats Lib "user32" () As Long

Sub Sample()
    If (CountClipboardFormats() = 0) = True Then
        MsgBox "Clipboard is empty"
    Else
        MsgBox "Clipboard is not empty"
    End If
End Sub
Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
Siddharth Rout
  • 147,039
  • 17
  • 206
  • 250
  • 1
    I would use `Dim DataObj As MSForms.DataObject` and `Set DataObj = MsForms.DataObject` (separate lines) to avoid auto-instancing variables. Also, a function would be best suited to grab clipboard text and return it. – JimmyPena Jan 26 '12 at 21:01
  • Would love to see a sample :) In fact would suggest you to post the entire code in a new post. It will help not only the OP but anyone else who comes looking for a solution :) – Siddharth Rout Jan 26 '12 at 21:14
  • No. I wouldn't recommend that ;) Reason being it can help someone new learn from it ;) Since you have given a suggestion why not follow it up in a new post? – Siddharth Rout Jan 26 '12 at 21:25
  • You have 99% of the code already, a second near-duplicate answer would be superfluous. – JimmyPena Jan 26 '12 at 21:27
  • Did you clear the clipboard as I suggested? If yes then you should be seeing the message "Data on clipboard is not text or is empty". I tested the code before posting :) – Siddharth Rout Jan 26 '12 at 21:32
  • I think the code you tested might have worked because the way it's written defines myString as a variant. I'm not sure that's the whole reason however because when I empty the clipboard with your code then I get this error: DataObject:GetText Invalid FORMATETC structure -2147221404 – Roy Jan 26 '12 at 21:40
  • I thought it did! I'm sure it worked at least once, and I thought I'd found the solution, but now it definitely doesn't matter. I've tried defining 2 variables, 1 string and 1 variant, and neither one works. Is there any way your "empty the clipboard" code could be tweaked to return a signal that the clipboard is already empty? – Roy Jan 26 '12 at 22:07
  • That works great Siddharth. It nicely bypasses the whole problem. – Roy Jan 26 '12 at 23:50
  • Siddharth, while you're still on the line, can you tell me the difference in these two declarations? `Private Declare Function OpenClipboard Lib "User32.dll" _ (ByVal hWndNewOwner As Long) As Long` and `Private Declare Function OpenClipboard Lib "user32" (ByVal Hwnd As Long) As Long` – Roy Jan 27 '12 at 00:02
  • There is no difference :) It is just a variable name differnce. You can even use this if you want Private Declare Function OpenClipboard Lib "User32.dll" _ (ByVal Roy As Long) As Long – Siddharth Rout Jan 27 '12 at 00:11
0

add the follwoing code just b4 the breaking line for debuging.... the error disapeared for me after that test.. wierd but it somehow works (Excel 2010)

myString = DataObj.GetText(1)
MsgBox myString
EEM
  • 6,601
  • 2
  • 18
  • 33
agadir
  • 1