Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 65
Default macro save as delete original file

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
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,123
Default macro save as delete original file

Why not use Name

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

--

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


"Pam M" wrote in message ...
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

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 62
Default macro save as delete original file

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

"Pam M" wrote:

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

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
2007 Macro to Open File, Delete Contents, Save New File Flintstone[_2_] Excel Discussion (Misc queries) 2 February 1st 10 11:25 PM
rename current file save and delete original valve79 Excel Programming 0 January 13th 06 09:17 PM
auto save excel file every 10 minutes to its original file name MEG Excel Discussion (Misc queries) 3 September 8th 05 07:12 PM
Auto save replaced my original file and now I need the original? Hols Excel Discussion (Misc queries) 1 August 15th 05 10:34 PM
VBA to Save a file to a different format but keep original groutch Excel Programming 4 December 2nd 04 10:48 PM


All times are GMT +1. The time now is 07:50 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"