Delete a file using VBA

Delete a file using VBA

Anonymous
Not applicable
764 Views
5 Replies
Message 1 of 6

Delete a file using VBA

Anonymous
Not applicable
Hello everyone,

Does anybody have an example of how to delete a file (in this case a text file). I have looked at the help, and can see the object.delete and object.deletefile methods but neither have an example for me to rip-off err! I mean to draw inspiration from!


Roy Meaden.
0 Likes
765 Views
5 Replies
Replies (5)
Message 2 of 6

Anonymous
Not applicable
Hi Roy,

Use the "Kill" statement.

From help ...

' Assume TESTFILE is a file containing some data.
Kill "TestFile" ' Delete file.

' In Microsoft Windows:
' Delete all *.TXT files in current directory.
Kill "*.TXT"
0 Likes
Message 3 of 6

Anonymous
Not applicable
Delete Method


Description

Deletes a specified file or folder.

Syntax

object.Delete force

The Delete method syntax has these parts:

Part Description
object Required. Always the name of a File or Folder object.
force Optional. Boolean value that is True if files or folders with the read-only attribute set are to be deleted; False (default) if they are not.
0 Likes
Message 4 of 6

Anonymous
Not applicable
Thanks for the reply, but call me Mr.Thickie, I do not know how to reference the file. I guess it would go something like (in pseudocode)

Dim filename as ?????
set filename = ??????
filename.delete true
set filename = nothing


Any help filling the blanks would be greatly appreciated!


Roy
0 Likes
Message 5 of 6

Anonymous
Not applicable
dim strg as string
dim fs as variant
Set fs = CreateObject("scripting.filesystemobject")
strg = "myfile.txt"
ChDir {where myfile is}
fs.deletefile strg1, 1
I believe the current directory must be set to the file location for
this to work.

I always use something like Kill "myfile.txt"

Tom
RoyMeaden wrote:

> Thanks for the reply, but call me Mr.Thickie, I do not know how to
> reference the file. I guess it would go something like (in pseudocode)
>
>
> Dim filename as ?????
> set filename = ??????
> filename.delete true
> set filename = nothing
>
> Any help filling the blanks would be greatly appreciated!
>
>
>
> Roy
0 Likes
Message 6 of 6

Anonymous
Not applicable
Thanks Tom ( And Gary )


Kill worked great. Another lesson learned.


Thanks


Roy
0 Likes