Error handling

J

Julia

Hi,

I have a template that has a user form...one of the options on the user form
allows the user to "paste from clipboard". However, if they accidentally
click this option without having anything on the clipboard, they get an
error. I want them to see a message box instead. So far I have:

Original code:

If frmSelect.boxPaste = True Then

Selection.HomeKey Unit:=wdStory
Selection.GoTo what:=wdGoToBookmark, Name:="bkRecName"
Selection.PasteAndFormat (wdPasteDefault)
Selection.Delete Unit:=wdCharacter, Count:=1

End If

I want to say:

If nothing is on the clipboard then instead of an error message _

Msgbox (“You must select and copy text firstâ€)

End if

Any ideas?? Thanks!
 
G

Greg Maxey

Julia,

Error handlers are not my strong suit by any means, but I will offer a stab.

On the line before the line in your code that causes the error (I assume
Selection.PasteAndFormat (wdPasteDefault))

Put a line like:

On Error GoTo Handler


Before the End Sub line add

Exit Sub
Handler:
If Err.Number = (put the error number that is being generated here) Then
MsgBox "Your Message"
Exit Sub
Else
Exit Sub
End If
 
G

Greg Maxey

Julia,

I had another look at your code. Just as a suggestion, you don't have to
use all of the selection statements.

Something like this may work just as well:

Sub Test()

If frmSelect.boxPaste = True Then
On Error GoTo ErrHandler
ActiveDocument.Bookmarks("bkRecName").Range.PasteAndFormat
(wdPasteDefault)
End If
Exit Sub
ErrHandler:
If Err.Number = 4605 Then
MsgBox "The clipboard is empty. You must first select and copy text."
Exit Sub
Else
MsgBox "Unknown error"
End If

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top