Deleting a file on C drive using VBA

T

Todd Huttenstine

I need to delete the file C:\Sheets.xls using VBA.
How do I do this?

Thank you
Todd Huttenstine
 
T

Todd Huttenstine

Below is the code I used and its closing the file, not
deleting the file.


Kill "c:\sheets.xls"
 
J

Jake

Untested air code (needs reference to Microsoft Scripting Runtime Library [scrrun.dll]):

Dim fso as FileSystemObject
Dim f as File

Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFile("C:\Sheets.xls")

f.Delete

Set f = Nothing
Set fso = Nothing

Hope this helps!

Jake
----- Todd Huttenstine wrote: -----

Below is the code I used and its closing the file, not
deleting the file.


Kill "c:\sheets.xls"
 
P

Pete McCosh

Todd,

The Kill command wont work if you have the file you want
to kill open, as I presume you must have, otherwise you
couldn't close it!

Pete
 
O

onedaywhen

Jake said:
Untested air code (needs reference to Microsoft Scripting Runtime Library [scrrun.dll]):

Dim fso as FileSystemObject
Dim f as File

Change these declarations to 'As Object' and the reference is not needed.
 
D

Dave Peterson

Actually, you can kill that open workbook:

This was posted by Jim Rech:

Sub testme()

With ThisWorkbook
.Saved = True
.ChangeFileAccess xlReadOnly
Kill .FullName
.Close False
End With

End Sub

(make sure you have backups!)
 
Top