rename, dont save over the top of files

C

cw

Hi all

Is there a way to check to see if a file on the desktop exists and if it
does rename it?

I ask this because I am putting together a vba macro to perform various mail
merges into one file and then save it on the desktop called 'new
report.doc'.

if poss I want as little user interaction as possible. I want to find a way
to check if new report.doc exists and if it does rename it to old
report.doc.

thanks in advance

Craig
 
K

Karl E. Peterson

cw said:
Is there a way to check to see if a file on the desktop exists and if it
does rename it?

Of course. Which part(s) of this task are you having problems with?
if poss I want as little user interaction as possible. I want to find a way
to check if new report.doc exists and if it does rename it to old
report.doc.

Just do something like this:

On Error Resume Next
Kill NewFile
Name OldFile As NewFile
On Error GoTo 0

Ruthless, but effective.
 
C

cw

Karl E. Peterson said:
Of course. Which part(s) of this task are you having problems with?


Just do something like this:

On Error Resume Next
Kill NewFile
Name OldFile As NewFile
On Error GoTo 0

Ruthless, but effective.

Thanks very much

I will give it a go this afternoon

Craig
 
C

cw

Gave it a go and it worked a treat


ended up with this

On Error Resume Next
Name "C:/documents and settings/all users/desktop/New Report.doc" As
"C:/documents and settings/all users/desktop/old Report.doc"
On Error GoTo 0
ActiveDocument.SaveAs FileName:="C:/documents and settings/all
users/desktop/New Report.doc"

got rid of the kill line as i dont want to delete anything

also what is the second on error for

i presume the first is just to say that if there is an error just resume on
the next line but i dont get what the second one is for

Thanks though
Craig
 
K

Karl E. Peterson

cw said:
Gave it a go and it worked a treat


ended up with this

On Error Resume Next
Name "C:/documents and settings/all users/desktop/New Report.doc" As
"C:/documents and settings/all users/desktop/old Report.doc"
On Error GoTo 0
ActiveDocument.SaveAs FileName:="C:/documents and settings/all
users/desktop/New Report.doc"

got rid of the kill line as i dont want to delete anything

Are you sure about that? What happens the 2nd time you run that code above?

(pause for dramatic effect <g>)

Okay, I'll tell ya... The Name statement will generate an error, because there's
already a file with the destination name -- the one you created the first time this
code ran. The net effect is, you only backup the initial bits, and never any
subsequent ones.
also what is the second on error for

i presume the first is just to say that if there is an error just resume on
the next line but i dont get what the second one is for

What DA said -- it turns off the error "handling" (ignoring, is really a better
word, in this case! <g>).
 

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