Mohd Muddser
Mohd Muddser

Reputation: 33

Creating an MS Excel file which deletes itself when a certain time or date is reached

Regarding MS Excel, I want to know how to create an Excel file that is deleted from the computer when a certain date or time is reached.

Is it possible? If it is, please tell me how it can be done?

Upvotes: 2

Views: 17195

Answers (2)

Dmitry Pavliv
Dmitry Pavliv

Reputation: 35863

You can call function DoSomething at a certain time in the future or by specifying the time interval by calling Activate_timer, but I don't think you can delete workbook direct from VBA. You can try to call in DoSomething function some batch file which closes the book first and then remove it.

Sub Activate_timer()
   ' at a certain time in the future
   'Application.OnTime DateSerial(2014,01,01)+TimeSerial(0,0,0), "DoSomething"
   ' or by specifying the time interval
   Application.OnTime Now + TimeValue("01:40:00"), "DoSomething"
End Sub

Sub DoSomething()
   'call the batch file
End Sub

Upvotes: 4

brettdj
brettdj

Reputation: 55692

If you add this code to the ThisWorkbook module, then the workbook will self-destruct if the current date is later than 1 Jan 2014.

Private Sub Workbook_Open()
If Now() > #1/1/2014# Then Call SuicideSub
End Sub

'

Sub SuicideSub()
'courtesy Tom Ogilvy
With ThisWorkbook
.Saved = True
.ChangeFileAccess xlReadOnly
Kill .FullName
.Close False
End With
End Sub

Upvotes: 7

Related Questions