# Simple Visual Basic if…then question

**URL:** <https://boards.straightdope.com/t/simple-visual-basic-if-then-question/533159>\
**Category:** Factual Questions\
**Created:** [March 20, 2010, 7:11pm UTC](https://boards.straightdope.com/t/simple-visual-basic-if-then-question/533159 "2010-03-20T19:11:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rhythmdvl](https://avatars.discourse-cdn.com/v4/letter/r/85f322/32.png) [@Rhythmdvl](https://boards.straightdope.com/u/Rhythmdvl)\
**Post date:** [March 20, 2010, 7:11pm UTC](https://boards.straightdope.com/t/simple-visual-basic-if-then-question/533159/1 "2010-03-20T19:11:42Z")

</div>

I have a simple macro that pastes unformatted text:

```auto

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

---

<div class="post-metadata">

**Author:** ![fubbleskag](https://avatars.discourse-cdn.com/v4/letter/f/c2a13f/32.png) [@fubbleskag](https://boards.straightdope.com/u/fubbleskag)\
**Post date:** [March 20, 2010, 7:34pm UTC](https://boards.straightdope.com/t/simple-visual-basic-if-then-question/533159/2 "2010-03-20T19:34:37Z")

</div>

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

```auto

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

```

---

<div class="post-metadata">

**Author:** ![code\_grey](https://avatars.discourse-cdn.com/v4/letter/c/f4b2a3/32.png) [@code\_grey](https://boards.straightdope.com/u/code_grey)\
**Post date:** [March 20, 2010, 7:40pm UTC](https://boards.straightdope.com/t/simple-visual-basic-if-then-question/533159/3 "2010-03-20T19:40:51Z")

</div>

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](http://www.vb-helper.com/tut6.htm) . If it doesn’t quite work, try googling for “vb on error error handling”
