PDA

View Full Version : Solved: Delete The File Itself



sheeeng
07-05-2005, 07:49 AM
Hi :hi:

Can a VBA macro execute to delete its own Excel file and then clear the command from the memory?

Just to find out whether this is possible...:friends:

Thx in advance. :hi:

Norie
07-05-2005, 08:02 AM
As far as I know No.

Try it


Kill ThisWorkbook.Path & "\" & ThisWorkbook.Name

sheeeng
07-05-2005, 08:05 AM
:doh: Sorry, the code does not work...

Error msg attached...

Please help. Thx. :friends:

Norie
07-05-2005, 08:12 AM
I know the code doesn't work, that's what I'm saying it isn't possible for a file to delete itself via VBA.

Justinlabenne
07-05-2005, 09:55 AM
From Tom Urtis:

Option Explicit

Sub SuicideSub()
MsgBox "Later!"
With ThisWorkbook
.Saved = True
.ChangeFileAccess xlReadOnly
Kill .FullName
.Close False
End With
End Sub

sheeeng
07-05-2005, 06:50 PM
Great.! Almost what I need...But can it close also the excel application...?

Thx.

Justinlabenne
07-05-2005, 07:18 PM
Option Explicit

Sub SuicideSub()
' Original code from Tom Urtis

MsgBox "I'm gonna do it!!!"

With ThisWorkbook

.Saved = True

.ChangeFileAccess xlReadOnly

Kill .FullName

Application.Quit

End With

End Sub

brettdj
07-05-2005, 07:31 PM
Weird

I submitted virtually the same code a couple of hours ago to the KB

Justinlabenne
07-05-2005, 07:34 PM
You mean this one don't you::*)


kb Entry (http://www.vbaexpress.com/kb/getarticle.php?kb_id=453&PHPSESSID=50610a6760539387b3728ff8e45fdb85)

sheeeng
07-05-2005, 07:35 PM
Aiyo...How come ar...I thought of combining this for one KB...Great that you have did it before me...

brettdj
07-05-2005, 07:55 PM
No, not the data table one, the suicidal workbook. Its still in the approval process

I saw the suicidal workbook cose around one year ago at Experts - Exchange and I'd bookmarked it, today I finally got around to submitting it and then I saw this thread. Did Tom Urtis actually claim authorship of this code or did you see him use it and then assign the credit it to him?

Cheers

Dave

Justinlabenne
07-05-2005, 08:14 PM
He has it in the book "VBA and Macros for Microsoft Excel" by Bill Jelen and Tracy Syrstad.

It's on page 301, and it does look a bit different, it is set to delete itself after a specific date, but it only about one line different. I have used various incarnations of this same code, like this:

Option Explicit

Private Sub Workbook_Open()
Dim n As String, x As String
n = "C:\Me.xls" 'Right here, put the file path where file should be located

If ThisWorkbook.FullName <> n Then

x = MsgBox("Re-Named files will self destruct", vbCritical, "Deleting File!!!")
With ThisWorkbook
.Saved = True
.ChangeFileAccess xlReadOnly
Kill ActiveWorkbook.FullName
Application.Quit
End With
Exit Sub
End If
End Sub


Which is pretty effective at keeping people from copy and moving files to other locations, so long as macros are enabled,

I just always give him credit, since I have learned loads from him.:thumb

ps: if the attachment isn't stored on the C:\ drive with the same name it given, it will prompt then die.

brettdj
07-05-2005, 08:20 PM
Thanks for the info.

I was concerned that I may have inadvertently lifted someone elses code, but the unattributed example I saw pre-dated the book. Much as yourself, If I do use code that had come from a website or can be credited to an individual I will list their name in the code (ie Jon Peltier laid much of the groundwork for exporting from Excel to PowerPoint)

Cheers

Dave

sheeeng
07-05-2005, 08:45 PM
Thanks for all your help! :friends:
There goes one of my intented KB..:doh:

sheeeng
07-05-2005, 08:47 PM
Option Explicit

Sub SuicideSub()
' Original code from Tom Urtis

MsgBox "I'm gonna do it!!!"

With ThisWorkbook

.Saved = True

.ChangeFileAccess xlReadOnly

Kill .FullName

Application.Quit

End With

End Sub

Great! :thumb
Another marked solved. :clap:

brettdj
07-06-2005, 03:12 AM
For completeness here is Suat's solution to the same problem. Code to be appreciated.


'----Code Start---
Sub SelfDel()
Dim tmpcode As String
Dim wrk As Workbook
Dim vbcomp As Object
tmpcode = tmpcode & "Sub FinishProcess(wrk As Workbook)" & vbCrLf
tmpcode = tmpcode & " ThisWorkbook.Worksheets(1).Cells(1, 1).Value = wrk.FullName" & vbCrLf
tmpcode = tmpcode & " Application.OnTime Now + TimeValue(""00:00:01""), ""KillerCode""" & vbCrLf
tmpcode = tmpcode & " wrk.Close False" & vbCrLf
tmpcode = tmpcode & "End Sub" & vbCrLf
tmpcode = tmpcode & "Sub KillerCode()" & vbCrLf
tmpcode = tmpcode & " Kill ThisWorkbook.Worksheets(1).Cells(1, 1).Value" & vbCrLf
tmpcode = tmpcode & " ThisWorkbook.Saved = True" & vbCrLf
tmpcode = tmpcode & " ThisWorkbook.Close False" & vbCrLf
tmpcode = tmpcode & "End Sub" & vbCrLf
Set wrk = Workbooks.Add
Set vbcomp = wrk.VBProject.VBComponents.Add(1)
vbcomp.CodeModule.AddFromString tmpcode
vbcomp.Name = "Killer"
Application.Run wrk.Name & "!Killer.FinishProcess", ThisWorkbook
End Sub
'----Code End----


Cheers

Dave

LVRebels
02-08-2009, 01:55 AM
So what is the code to creat a self-destructing file after a certain time? I'm not in your league guys, just an excel user.