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?
Handle the errors with
On Error GoTo
as shown here: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 atC:\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.
EDIT: you may also determine if the clipboard is empty by using this code: