PDA

View Full Version : Solved: delete open workbook



peacemaker
04-03-2009, 10:02 AM
I am attempting to delete an open workbook by copying VBA code into a new workbook that upon opening, closes the original workbook ("E-Sub Slip.xls"), deletes it, and then closes the new workbook without saving.

In order to accomplish this I have a hidden worksheet (called "DontOpen") in the original workbook that has the following text:
Private Sub Worksheet_Activate()

Set wbk = Workbooks("E-Sub Slip.xls")
wbk.Activate
ActiveWindow.Close (False)

response = MsgBox("Delete E-Sub Slip.xls on C:\ drive?", vbYesNo, "")
If response = vbYes Then
On Error Resume Next
Kill "C:\E-Sub Slip.xls"
On Error GoTo 0
Else:
End If

ActiveWindow.Close (False)

End Sub
the original document used this code to execute the above:
Sheets("DontOpen").Visible = True
Sheets("DontOpen").Copy
So far I am successful in running the code but the "wkb.Activate" and the "ActiveWindow.Close (false)" code does nothing (not even an error)

What do I need to do differently for this to work?
Thanks,
peacemaker

Bob Phillips
04-03-2009, 01:20 PM
Sub DeleteActiveWorkbook()
On Error GoTo ErrorHandler
With ActiveWorkbook
If .Path <> "" Then
.Saved = True
.ChangeFileAccess xlReadOnly
Kill ActiveWorkbook.FullName
End If
End With
Exit Sub

ErrorHandler:
MsgBox "Fail to delete file: " & ActiveWorkbook.FullName
Exit Sub

End Sub

peacemaker
04-03-2009, 02:45 PM
This works. Thank you very much!