Link to home
Start Free TrialLog in
Avatar of mtrussell
mtrussell

asked on

Deleting a file from a folder using VBA

This question is probably pretty straight forward but given the consequences if I get it wrong I thought I would ask.

I have a table that has the name of all the files in a particular folder on my c: drive.  If I delete it out of the table I want it to delete the file out of the folder too.  I have the functionality to import the file names and to delete the file name out of the table I need to delete the file now out of the folder.  

How would I do this.  I get once the file is deleted it is gone, so that is why I am asking.
ASKER CERTIFIED SOLUTION
Avatar of danishani
danishani
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Jeffrey Coachman
Just a note, that in some situations, "Kill" is prohibited in VBA because of certain security settings...

In this case you will have load a reference to the Windows Scripting Runtime Library in your VBA editor and use code like this:

Dim fso As FileSystemObject
    Set fso = CreateObject("Scripting.FileSystemObject")
    fso.DeleteFile ("C:\YourFolder\YourFile.xls")



JeffCoachman
No points for this, please :)

You can run Jeff's code above without setting a reference by changing:

Dim fso As FileSystemObject

to

Dim fso As Object

Patrick
Hi Patrick:  I have to ask: "Why would you want to?"  It has aspects of using a table saw without a guard ;-)
Ray,

1) Laziness :)

2) Not really applicable for the Scripting Runtime library, but I try to do most of my external references using late binding so as to keep maximum flexibility if there may be a range of versions in use

Patrick
Avatar of mtrussell
mtrussell

ASKER

thanks
<...range of versions in use> -  Thanks, completely overlooked that.