To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here
VBForums  

VB Wire News
Article :: Building Dynamic Systems with Expressions in .NET
How Is XML Like An Interface?
Understanding Covariance and Contravariance
Print VS 2010 Keyboard Shortcut References in Letter (8.5x11in) and A4 (210×297mm) Sizes
Updated Productivity Power Tools



Go Back   VBForums > Visual Basic > Office Development

Reply Post New Thread
 
Thread Tools Display Modes
Old Mar 17th, 2010, 06:37 AM   #1
Resource Dragon
Member
 
Join Date: Jun 09
Location: Townsville, Qld, Australia
Posts: 60
Resource Dragon is an unknown quantity at this point (<10)
Resolved [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.
Resource Dragon is offline   Reply With Quote
Old Mar 17th, 2010, 08:16 AM   #2
koolsid
Comfortably Numb
 
koolsid's Avatar
 
Join Date: Feb 05
Location: Mumbai, India
Posts: 9,300
koolsid is a name known to all (1000+)koolsid is a name known to all (1000+)koolsid is a name known to all (1000+)koolsid is a name known to all (1000+)koolsid is a name known to all (1000+)koolsid is a name known to all (1000+)koolsid is a name known to all (1000+)koolsid is a name known to all (1000+)koolsid is a name known to all (1000+)koolsid is a name known to all (1000+)
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 "Resolved", if the query is solved...
'~~> If a post has helped you then Please Rate it by Clicking "Rate This Post" on the left hand side of the post!
VB 6.0 Code Generator for VB6, Working with Windows Registry, Creating Access Database via Code, Listview Sub Item Image, Common Dialog Control
VBA 6.5
Excel Data Validation, Conditional Formating, Pivot Table, Column No to Column Name, Scroll Bars, Music Player, Creating Splash Screen
Powerpoint Creating Charts
Misc Ascii Table, Chit Chatters, USB Pendisk - Trojans, MyFlickr

MyGear: Sony VGN-FZ27G with a triple boot between (XP + Office 2003 + VB6), (VISTA + Office 2007 + VS2008) and (Win7 + Office 2010 + VS2010)
koolsid is offline   Reply With Quote
Old Mar 18th, 2010, 06:36 AM   #3
Resource Dragon
Member
 
Join Date: Jun 09
Location: Townsville, Qld, Australia
Posts: 60
Resource Dragon is an unknown quantity at this point (<10)
Smile 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
Resource Dragon is offline   Reply With Quote
Old Mar 18th, 2010, 03:40 PM   #4
westconn1
PowerPoster
 
Join Date: Dec 04
Posts: 13,203
westconn1 is a splendid one to behold (700+)westconn1 is a splendid one to behold (700+)westconn1 is a splendid one to behold (700+)westconn1 is a splendid one to behold (700+)westconn1 is a splendid one to behold (700+)westconn1 is a splendid one to behold (700+)westconn1 is a splendid one to behold (700+)
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:
  1. fname = activeworkbook.fullname
  2. activeworkbook.close false
  3. 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
westconn1 is offline   Reply With Quote
Reply

Tags
delete, files

Go Back   VBForums > Visual Basic > Office Development


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 08:05 PM.





Acceptable Use Policy

Internet.com
The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers

Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.