Problem solve Get help with specific problems with your technologies, process and projects.

Creating VB scripts that can delete files on the hard drive

Here our experts show how to create VB scripts that can delete files on the hard drive.

I'm developing a simple VB script in MS Excel that will, amongst other things, delete a file on the harddrive, e.g c:\dir\file.txt. However, I seem to have limited success with this seemingly simply task. I've tried using the KILL ("filename") command but could not do so either. Please help!
I'm not sure why you are having problems with the KILL-command. However, I have created an example that will allow you to see how you might pull this off. Here are the steps to reproduce my example.

1. Open Excel.
2. Choose "Tools->Macro->Visual Basic Editor"
3. Right click the ThisWorkbook item and choose "View Code"
4. Add the following code:

  Private Sub Workbook_Open()
      Dim dialog As DeleteFileForm     
      Set dialog = New DeleteFileForm     
      dialog.Show (vbModeless)     
      Set dialog = Nothing     
  End Sub
5. Right click the Microsoft Excel Objects item and Insert a UserForm. Change its name to DeleteFileForm.
6. Right click the DeleteFileForm item and choose "View Code".
7. Add the following code:
  Private Sub buttonDelete_Click()
  On Local Error GoTo ErrHandler
      Kill (textBox.Text)
  Exit Sub
      Debug.Print (Error)
  End Sub
7. Right click the DeleteFileForm and choose "View Object".
8. Add a lable, textbox and button to the form. Name the textbox "textBox", and the button "buttonDelete".
9. Save the macro and Excel file to persist your changes.
10. Press the F5 key to begin debugging your macro!

Dig Deeper on Win Development Resources

Have a question for an expert?

Please add a title for your question

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

Start the conversation

Send me notifications when other members comment.

Please create a username to comment.