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 / September 2007

Tip: Looking for answers? Try searching our database.

macro save as delete original file

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Pam M - 25 Sep 2007 19:40 GMT
I would like to write a macro to save a file to a different folder location,
same filename, and then delete the file from the original location.  I tried
record macro and went to file-open to do the delete file part (right click on
file and delete), but the file didn't delete.  In visual basic, the command
recorded was ChDir and the filename.  Any help would be appreciated.  Pam
Ron de Bruin - 25 Sep 2007 20:09 GMT
Why not use Name

http://www.rondebruin.nl/folder.htm

Signature

Regards Ron de Bruin
http://www.rondebruin.nl/tips.htm

>I would like to write a macro to save a file to a different folder location,
> same filename, and then delete the file from the original location.  I tried
> record macro and went to file-open to do the delete file part (right click on
> file and delete), but the file didn't delete.  In visual basic, the command
> recorded was ChDir and the filename.  Any help would be appreciated.  Pam
Keithlo - 25 Sep 2007 20:24 GMT
I tested the code below and it worked for me.

You should definitely test it out before you use it on live data.

The relevant code for deleting the file is from VBScript (I googled that to
find the code snippet).  VBScript is compatible with VBA, and it allows one
to operate on windows objects, like files and folders.  Some VBScript Help is
available in VBA Help, but not all.

Sub DeleteFile()

Dim MyFullName
Dim fileSaveName

MyFullName = ActiveWorkbook.FullName

fileSaveName = Application.GetSaveAsFilename( _
   fileFilter:="Excel Files (*.xls), *.xls")
If fileSaveName <> False Then
   ActiveWorkbook.SaveAs fileSaveName
   Set objFSO = CreateObject("Scripting.FileSystemObject")
   objFSO.DeleteFile (MyFullName)
End If

End Sub

Note that if you click Cancel on the Save As dialog box, nothing happens.  
It's only if you have given a Save As name that the file will be saved and
the original deleted.  You could add a warning that file MyFullName is about
to be deleted, etc.  That would take some extra coding and might wind up
being a pain to respond on each time.

Hope this helps.

Keith

> I would like to write a macro to save a file to a different folder location,
> same filename, and then delete the file from the original location.  I tried
> record macro and went to file-open to do the delete file part (right click on
> file and delete), but the file didn't delete.  In visual basic, the command
> recorded was ChDir and the filename.  Any help would be appreciated.  Pam
 
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.