Catching duplicate filenames when renaming a file

G

General

Hi,

In my code I have to rename some files, using the command:

Name oldfilename as newfilename

Sometimes the newfilename already exists so I am looking for a way to
detect that condition, I tried:

If iserror(Name oldfilename as newfilename) then
CODE HERE
else
name oldfilename as newfilename
end if

However, Excel "Expects list seperator or )" around the oldfilename in
the iserror statement. Help!

Also, for this code I am in the middle of an application filesearch so
I can't search for the file using application.filesearch.

Thank you!!!

Phil
 
B

Bob Phillips

Another way

On Error Resume Next
Name oldfilename As newfilename
If Err.Number = 58 Then 'already exists
MsgBox "file " & newfilename & " already exists"
End If
On Error GoTo 0


--

HTH

RP
(remove nothere from the email address if mailing direct)
 

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