Results 1 to 4 of 4
  1. #1
    Gold Lounger
    Join Date
    Feb 2004
    Location
    Cape Town, RSA
    Posts
    3,444
    Thanks
    0
    Thanked 1 Time in 1 Post

    Delete Files in Dir. (Word 2000)

    Hi all,

    As a part of a VBA project i'm dealing with, I need some code to delete files in a certain directory who's create property is older than 1 year. I need help with 2 things:

    1. How do I access the Create Date from the File | Properties Dialog?
    2. How do I delete the file?

    The code I have is as follows:
    <pre>Option Explicit

    Sub DeleteOldFiles()
    Dim myPath As String
    Dim myName As String
    myPath = "C:WordFiles"
    myName = Dir(myPath, vbNormal)
    Do While myName <> ""
    If Word.wdPropertyTimeCreated < _
    DateSerial(Year(Date), _
    Month(Date - 12), _
    Day(Date)) Then
    File.Delete '(This is obviously wrong!!!)
    End If
    myName = Dir
    Loop
    End Sub</pre>


    Thx for any assistance!!
    Regards,
    Rudi

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Delete Files in Dir. (Word 2000)

    You can use this:

    Sub DeleteOldFiles()
    Dim fso As Object
    Dim fld As Object
    Dim fil As Object

    Set fso = CreateObject("Scripting.FileSystemObject")
    Set fld = fso.GetFolder("C:WordFiles")

    For Each fil In fld.Files
    If fil.DateCreated < DateAdd("yyyy", -1, Date) Then
    fil.Delete
    End If
    Next fil

    Set fil = Nothing
    Set fld = Nothing
    Set fso = Nothing
    End Sub

  3. #3
    Gold Lounger
    Join Date
    Feb 2004
    Location
    Cape Town, RSA
    Posts
    3,444
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Delete Files in Dir. (Word 2000)

    Wow...thx, it works like a charm!

    Now for the obvious questions/statements:
    1. I see that CreateObject is a function. In Word help, it only lists "CreateObject("Excel.Sheet") and CreateObject("Excel.Application")" as arguments. Based on these examples can I assume you can use any application name and class to create a reference for an object?
    2. Scripting.FileSystemObject is an object, and I see its referenced under the Also See link of CreateObject. I would have never guessed that this could be used to refer to a Word File. I tried ("Word.Documents") and it errored saying ActiceX could not create this object...(Could also be that it is a collection!)
    3. It's interesting to note that you used DateAdd to create a reference for the fil variable to delete. I suppose it doesn't matter which function is used, as long as it gives a correct reference to the variable.

    Thanx again!
    Regards,
    Rudi

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Delete Files in Dir. (Word 2000)

    Scripting is an object library that is available on all recent systems. It contains objects, properties and methods that can be used to manipulate folders and files. It has nothing to do with Word as such, it is a general library.

    The code I posted uses a technique called late binding, i.e. the variables are declared as Object instead of as a specific type. That way, you don't need to set a reference to the library in Tools | References...
    I could also have used Early Binding, by selecting Tools | References... and ticking the check box for Microsoft Scripting Runtime, then clicking OK. I could then have declared the objects as follows:

    Dim fso As New Scripting.FileSystemObject
    Dim fld As Scripting.Folder
    Dim fil As Scripting.File

    The line Set fso = CreateObject("...") wouldn't have been necessary then. If you use early binding, you can use IntelliSense, i.e. if you type fso followed by a point (period), a list of available properties and methods will pop up.

    I don't use DateAdd to create a reference to the fil variable, I use it to test if the CreateDate property of the file is more than a year before the current date.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •