|
-
Mar 17th, 2010, 06:37 AM
#1
Thread Starter
Addicted Member
[RESOLVED] Delete File
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.
-
Mar 17th, 2010, 08:16 AM
#2
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 exercise for the Heart is to bend down and help another up...
Please Mark your Thread " Resolved", if the query is solved
MyGear:
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero
-
Mar 18th, 2010, 06:36 AM
#3
Thread Starter
Addicted Member
Re: Delete File
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
-
Mar 18th, 2010, 03:40 PM
#4
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:
fname = activeworkbook.fullname activeworkbook.close false kill fname
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 for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|