Run-time error 2001

R

Robert

I am currently implementing a macro that prompts the user
for a source and a destination to be copied to. In the
case that the user gives a destination that is already in
use, I do not want the copy to occur and a form should pop
up warning them of the mistake. Everytime a proper and
clear destination is given, I get no errors. However,
when I enter an occupied space as the destination for the
data, I get an error stating {Run-time error '2001': You
canceled the previous operation.} The functionality of
this macro is sound ... it works fine and the data is not
overwritten. I would like, though, to be able to get rid
of this error box (which prompts whether you want to End
or Debug the error). Any suggestions?

Thanks in advance!

Robert
 
B

Brendan Reynolds \(MVP\)

That's one of the many limitations of macros, Robert - no error trapping.

Here's how I might do it in code:

Public Sub CopyFile(ByVal strSource As String, ByVal strTarget As String)

If Dir(strTarget) = vbNullString Then
FileCopy strSource, strTarget
Else
MsgBox "The target file already exists. Please choose another name
for the target file."
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