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 Sub5. 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 ErrHandler: Debug.Print (Error) End Sub7. 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
Related Q&A from Mark Belles
This expert response shows how to access VB .NET Windows control on the client side. Continue Reading
Have a question for an expert?
Please add a title for your question
Get answers from a TechTarget expert on whatever's puzzling you.