|
|
#1 |
|
Member
Join Date: Jun 09
Location: Townsville, Qld, Australia
Posts: 60
![]() |
How do I use Visual Basic code to delete a file? The VBA documentation on “Delete” hasn’t helped me. The story behind my need for such code follows here:
I have a shared excel workbook (with a lot of VB code for forms &c) and I have written a routine (again, this routine lives in the shared workbook’s macros) that creates another workbook which is then populated with selected data from the original shared workbook. The new file is then saved to another part of a shared drive. The name of the new file depends upon the data that is chosen to populate it. To do this, when I first create the new workbook, I call it “Interim Cert” and it is saved in the same folder as the shared file. When the Interim Cert is fully populated, the code then picks up the file name that is to be used and works out where the file is to be saved – there are 12 possible folders, depending upon the month. I do this using the SaveAs command. My problem is that I’ve got a file called Interim Cert.xls to dispose of. The following code, based on Koolsid’s answer to the question, “How do I delete a file while it is open?” is satisfactory if I am only creating one new file from the original shared file but doesn’t work if I am creating half a dozen. Workbooks.Open ("G:\Resources and Publications\Finance 09 10\Interim Cert.xls") Workbooks("Interim Cert.xls").Activate With ActiveWorkbook .Saved = True .ChangeFileAccess xlReadOnly Kill ActiveWorkbook.FullName Workbooks("Interim Cert.xls").Close End With So, can someone please tell me how to delete the file? Alternatively, can someone tell me how to rename and move the file? (The whole problem arises because the original file is shared. When I started writing the code, I simply added another worksheet, then moved it into its own workbook.) Thank you. |
|
|
|
|
|
#2 |
|
Comfortably Numb
Join Date: Feb 05
Location: Mumbai, India
Posts: 9,300
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
Re: Delete File
You can delete files using the API SHFileOperation. Search the forum and you will find plenty of examples on this API.
__________________
A good excercise for the Heart is to bend down and help another up... Please Mark your Thread " '~~> If a post has helped you then Please Rate it by Clicking "Rate This Post" on the left hand side of the post! Excel Powerpoint MyGear: Sony VGN-FZ27G with a triple boot between (XP + Office 2003 + VB6), (VISTA + Office 2007 + VS2008) and (Win7 + Office 2010 + VS2010) |
|
|
|
|
|
#3 |
|
Member
Join Date: Jun 09
Location: Townsville, Qld, Australia
Posts: 60
![]() |
G’day Koolsid. I hadn’t heard of API SHFileOperation before, which was why I hadn’t looked for it. I followed up your suggestion and came across http://www.techbookreport.com/tutorials/fso1.html which explained the concept in easy steps for a beginner.
I’ve got code for a deletion working in a practice file. I haven’t had a chance to run the code in the live file yet but I think it should work. Thanks for the suggestion. Cheers Resource Dragon |
|
|
|
|
|
#4 |
|
PowerPoster
Join Date: Dec 04
Posts: 13,203
![]() ![]() ![]() ![]() ![]() ![]() ![]() |
Re: [RESOLVED] Delete File
you can't really delete the workbook while it is open in excel, you need to close the workbook first, of course if the code is n that workbook it stops running
vb Code:
__________________
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case. Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part come back and mark your original post as resolved if your problem is fixed pete |
|
|
|
![]() |
| Tags |
| delete, files |
|
||||||
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|