Method 1: Only works in Excel
 
Option Explicit
 
Public Sub killSelf()
   With ThisWorkbook
        .Saved = True
        .ChangeFileAccess xlReadOnly
        Kill .FullName
        .Close
    End With
End Sub
 
 
Method 2: Works in normal vb and vba, but need administrator's authority with windows2000 or over.
 
Option Explicit
 
Private Declare Function MoveFileEx Lib "kernel32" Alias "MoveFileExA" (ByVal OldFilename As String, ByVal NewFileName As String, ByVal nWord As Long) As Long
Private Const MOVEFILE_DELAY_UNTIL_REBOOT = &H4
Private Const MOVEFILE_REPLACE_EXISTING = &H1
Private Const MOVEFILE_DELAY_AND_REPLACE = MOVEFILE_DELAY_UNTIL_REBOOT + MOVEFILE_REPLACE_EXISTING
 
'Deletes the file szSource after reboot, use full path names
Public Sub DeleteAfterReboot()
    Call MoveFileEx(ThisWorkbook.FullName, vbNullString, MOVEFILE_DELAY_AND_REPLACE)
End Sub

0 件のコメント:
コメントを投稿