Results 1 to 7 of 7
  1. #1
    5 Star Lounger
    Join Date
    Mar 2001
    Location
    New York, NY
    Posts
    922
    Thanks
    2
    Thanked 12 Times in 11 Posts

    Folder Contents (Word XP - SP2)

    What is the best way to retireve the contents of a specified folder to prepare a list of files in Word? I was expecting to use the MS Scripting Runtime reference, but there seems to be no help file available.

    Thanks,
    Richard Barrett

  2. #2
    5 Star Lounger
    Join Date
    Jul 2002
    Location
    Toronto, Ontario, Canada
    Posts
    1,139
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Folder Contents (Word XP - SP2)

    Richard,

    If you don't have your heart set on the Scripting runtime, you could use something like this:

    Dim strFile As String

    strFile = Dir("c:fullpathtodirectory*.doc", vbNormal)

    Do While strFile <> ""
    Debug.Print strFile
    strFile = Dir()
    Loop
    --
    Bryan Carbonnell - Toronto <img src=/S/flags/Ontario.gif border=0 alt=Ontario width=30 height=18> <img src=/S/flags/Canada.gif border=0 alt=Canada width=30 height=18>
    Unfortunately common sense isn't so common!!
    Visit my website for useful Word, Excel and Access code, templates and Add-Ins

  3. #3
    5 Star Lounger
    Join Date
    Mar 2001
    Location
    New York, NY
    Posts
    922
    Thanks
    2
    Thanked 12 Times in 11 Posts

    Re: Folder Contents (Word XP - SP2)

    That's great... Thanks.

    One small problem... it all prints as one string. If I've specified a specific file extension, it's easy enough to search and replace, adding a paragraph. How would you suggest handling a *.* situation where that's not possible?

    I certainly didn't have my heart set on the Scripting Runtime, but I thought perhaps there was a Files collection that I could use and manipulate a little more easily.

    Thanks,
    Richard

  4. #4
    5 Star Lounger
    Join Date
    Jul 2002
    Location
    Toronto, Ontario, Canada
    Posts
    1,139
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Folder Contents (Word XP - SP2)

    If you are adding the list of files to a Documents, instead of the debug.print line, you could use:

    Selection.InsertAfter StrFile & vbcrlf

    That should insert the file name and a paragraph mark after the current selection.
    --
    Bryan Carbonnell - Toronto <img src=/S/flags/Ontario.gif border=0 alt=Ontario width=30 height=18> <img src=/S/flags/Canada.gif border=0 alt=Canada width=30 height=18>
    Unfortunately common sense isn't so common!!
    Visit my website for useful Word, Excel and Access code, templates and Add-Ins

  5. #5
    5 Star Lounger
    Join Date
    Mar 2001
    Location
    New York, NY
    Posts
    922
    Thanks
    2
    Thanked 12 Times in 11 Posts

    Re: Folder Contents (Word XP - SP2)

    Easy enough. I guess I should have played with it more.... I was thinking that strFile was the combined list of file names. I see it's not.

    Thanks.

  6. #6
    5 Star Lounger
    Join Date
    Jul 2002
    Location
    Toronto, Ontario, Canada
    Posts
    1,139
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Folder Contents (Word XP - SP2)

    Every time you issue a Dir command, it gets the next file in the directory that matches the pattern, if you have a pattern.

    So, strFile is always the most recent file that was returned by the Dir command.

    The help file in W2K has quite a good description of the Dir command.

    But, I'm glad this worked for you.
    --
    Bryan Carbonnell - Toronto <img src=/S/flags/Ontario.gif border=0 alt=Ontario width=30 height=18> <img src=/S/flags/Canada.gif border=0 alt=Canada width=30 height=18>
    Unfortunately common sense isn't so common!!
    Visit my website for useful Word, Excel and Access code, templates and Add-Ins

  7. #7
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Folder Contents (Word XP - SP2)

    A third method, in addition to Dir or the File Scripting Object, is to the use the MS Office FileSearch object - very simple to use and you should be able to find the online help for it. Also, a search here for 'FileSearch' should turn up some usable code.

    Gary

Posting Permissions

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