Simple Visual Basic if…then question

I have a simple macro that pastes unformatted text:



Selection.PasteSpecial Link:=False, DataType:=wdPasteText, Placement:= _
        wdInLine, DisplayAsIcon:=False

But if the clipboard is empty or contains something other than text (e.g., a picture), I get a run-time error telling me the specified data type is unavailable.

I’d like to wrap the macro in a simple if…then statement that first checks for the correct data type and then either runs the rest of the code or just beeps if it can’t. I don’t need it to tell my why it can’t run (i.e., I don’t want to click away an alert) or anything — even doing nothing would suffice.

Is there a simple way of doing this?

Thanks,

Rhythm

Off the top of my head, I think you can create an object from your clipboard and then check it (below not tested):



Dim MyData as DataObject
MyData.GetFromClipboard
If MyData.GetFormat(1) = True Then
  ... do stuff with MyData.GetText(1)
End If


maybe use error handling for simplicity?

On Error Resume Next
Selection.PasteSpecial Link:=False, DataType:=wdPasteText, Placement:= wdInLine, DisplayAsIcon:=False

I am not sure if the above is precisely correct, but I can infer this much for discussion here VB Helper Tutorial: Bug Proofing - Error Handling Fundamentals . If it doesn’t quite work, try googling for “vb on error error handling”