vba 创建一个 MS Excel 文件,该文件会在到达特定时间或日期时自行删除
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20844175/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating an MS Excel file which deletes itself when a certain time or date is reached
提问by Mohd Muddser
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.
关于MS Excel,我想知道如何创建一个在达到某个日期或时间时从计算机中删除的Excel文件。
Is it possible? If it is, please tell me how it can be done?
是否可以?如果是,请告诉我怎么做?
回答by brettdj
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.
如果将此代码添加到ThisWorkbook
模块中,则如果当前日期晚于 2014 年 1 月 1 日,则工作簿将自毁。
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
回答by Dmitry Pavliv
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 filewhich closes the book first and then remove it.
您可以DoSomething
在将来的某个时间调用函数或通过调用指定时间间隔Activate_timer
,但我认为您不能直接从 VBA 中删除工作簿。您可以尝试在DoSomething
函数中调用一些批处理文件,该文件首先关闭书籍,然后将其删除。
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