Need VBA to rename a file on my directory-URGENT


Posted by Bill Ford on October 06, 2000 8:17 PM

I need Excel VBA macro to rename a file that exists on my D directory every time it is run....ie.

have a file called backup.html
need to rename to working.html

appreciate any ideas....urgent

Posted by Ivan Moala on October 07, 2000 7:14 PM

Bill
Something like this should help you out
without shelling......

Sub ReName_File()
Dim OldName As String
Dim NewName As String


OldName = "D:\t.txt": NewName = "D:\ok.txt"
Name OldName As NewName
End Sub

HTH


Ivan

Posted by Bill Ford on October 08, 2000 8:01 AM


Thanks Ivan

That almost worked...get a "runtime error 58" "File already exists" error, so need some code to ignore that error and continue...

Thanks for your help...
Bill

Posted by Bill Ford on October 08, 2000 1:54 PM

How do I ignore "runtime error 58" "File already exists" error??



Posted by Ivan Moala on October 08, 2000 11:52 PM

Re: How do I ignore "runtime error 58" "File already exists" error??

Bill here is how I have used it;

Sub ReName_File()
Dim OldName As String
Dim NewName As String
Dim IsThere, Exists As Boolean

EnterAgain:
OldName = Application.InputBox("Enter name of File to change", "Change name of File", "C:\", Type:=2)
If OldName = "False" Then End

IsThere = Dir(OldName)
If IsThere = "" Then MsgBox "File does not exist!": GoTo EnterAgain
Exists = True

EnterAgain1:
NewName = Application.InputBox("Enter New name of File", "Change name of File", "C:\", Type:=2)
If NewName = "False" Then End

IsThere = Dir(NewName)
If IsThere <> "" Then MsgBox "FileName already exists!" & Chr(13) & _
"You need to enter a new name": GoTo EnterAgain1
Exists = True

'OldName = "D:\t.txt": NewName = "D:\ok.txt"
On Error Resume Next
Name OldName As NewName
If Err.Number <> 0 Then MsgBox Err.Number & " " & Err.Description
End Sub

HTH

Ivan