Delete Temp (.tmp) and Word Backup files (.wbk) using Word VBA
Temp and Backup files add unnecessary space on disk. It is good to delete them after you are sure of the original file. The following VBA function deletes/clears the temp and backup files from a specified folder. This function cannot be used in Word 2007 as Application.Filesearch is not supported
Function DeleteBackupsandTempFiles(ByVal Deletepath As String)
Dim fs1 As Object
fs1 = CreateObject("scripting.filesystemobject")
file_path = Deletepath
'--- Deletes all temp files in the particular folder
With Application.FileSearch
.LookIn = file_path
.filename = ".tmp"
.SearchSubFolders = False
.Execute()
If .FoundFiles.Count > 0 Then
For k1 = 1 To .FoundFiles.Count
On Error GoTo DeleteFileError
fs1.deletefile.FoundFiles(k1)
Next k1
End If
End With
Exit Function
DeleteFileError:
If Err.Number = 70 Then
Err.Clear()
Resume Next
Else
Err.Clear()
Resume Next
End If
End Function
Be cautious while deleting Backup files and Temp files, Word creates these files when the document is Open for editing. Make sure you run this procedure in folders where you have completed work
No comments:
Post a Comment