Sunday, July 20, 2008

Clean Temp (.tmp) and Word Backup files (.wbk) using Word VBA

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

To kill normal temp files please refer http://vbadud.blogspot.com/2007/04/delete-temporary-files.html. To get the location of Temporary folder please us http://vbadud.blogspot.com/2007/04/visual-basic-function-to-get-temporary.html

Application.Filesearch is not supported in 2007 (Excel/Word etc).

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

StumbleUpon
Share on Facebook
Related Posts Plugin for WordPress, Blogger...

Visual Basic for Applications (VBA) Forum (recent threads)

CodeKeep VBA Feed

Visual Studio Tools for Office Forum (recent threads)

Download Windows Live Toolbar and personalize your Web experience! Add custom buttons to get the information you care about most.

Office Business Applications (OBA) Team Blog

MSDN Code Gallery Published Resources For Tag VSTO

microsoft.public.vsnet.vstools.office Google Group