Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Excel / Programming / January 2007

Tip: Looking for answers? Try searching our database.

Move and rename a file

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Peter - 26 Jan 2007 12:24 GMT
Calling all Excel programming brains,

I am trying to move and rename a file but don't know where to start,
can anybody help?

Thanks,
Peter
ivanmoala@xtra.co.nz - 26 Jan 2007 12:59 GMT
Lookup "Name Statement" in VBA Help

Dim OldName, NewName
OldName = "OLDFILE": NewName = "NEWFILE"    ' Define file names.
Name OldName As NewName    ' Rename file.

OldName = "C:\MYDIR\OLDFILE": NewName = "C:\YOURDIR\NEWFILE"
Name OldName As NewName    ' Move and rename file.

> Calling all Excel programming brains,
>
[quoted text clipped - 3 lines]
> Thanks,
> Peter
Chip Pearson - 26 Jan 2007 13:23 GMT
Peter,

Depending on what you actually want to do, try something like

Sub AAA()
Dim DestinationFolder As String
Dim SourceFileName As String
Dim DestinationFileName As String

DestinationFolder = "C:\Test" '<<< CHANGE
SourceFileName = "C:\Test1.txt" '<<< CHANGE
DestinationFileName = "C:\Test\Test2.txt" '<<< CHANGE

If Dir(DestinationFolder, vbDirectory + vbHidden) = vbNullString Then
   ' folder does not exist, create it
   MkDir DestinationFolder
End If

'''''''''''''''''''''''''''''''
' If you want to COPY the file,
'''''''''''''''''''''''''''''''
FileCopy SourceFileName, DestinationFileName

'''''''''''''''''''''''''''''''
' If you want to MOVE the file.
'''''''''''''''''''''''''''''''
Name SourceFileName As DestinationFileName
End Sub

You'll probably want to add some error checking to ensure the file exists,
and the destination file does not exist, but the code above should get you
started.

Signature

Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
(email on the web site)

> Calling all Excel programming brains,
>
[quoted text clipped - 3 lines]
> Thanks,
> Peter
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.