only need to see certain file types

T

Tony Logan

I'm trying to find a way to gather a list of all files of a certain type in a
folder. For example, let's say folder "Work" has three .doc files and two
..txt files. I'd like to be able to gather a list of only the .doc files.

Can the code below, which was copied from Word's Visual Basic Help
files(under "Files Collection"), be modified to do what I want? And what is
"folderspec"?

Here's the code:

Sub ShowFolderList(folderspec)
Dim fs, f, f1, fc, s
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFolder(folderspec)
Set fc = f.Files
For Each f1 in fc
s = s & f1.name
s = s & vbCrLf
Next
MsgBox s
End Sub

I wrote an additional macro to call the above subroutine:

Sub Call_File_List()
Call ShowFolderList("C:\Data\Edgar PPT\4Q03\")
End Sub

Thanks.
 
S

Steve Lang

Hi Tony,

How about this. It opens each file that ends with ".doc":
Sub Foo
Dim i As Long

With Application.FileSearch

'Search in foldername

.LookIn = "C:\Test"

.SearchSubFolders = False

.FileName = "*.doc"

.Execute

For i = 1 To .FoundFiles.Count

Documents.Open FileName:=(.FoundFiles(i))

insert sub or function here



'Close document and save changes

If ActiveDocument.Saved = False Then _

ActiveDocument.Close savechanges:=wdSaveChanges Else _

ActiveDocument.Close savechanges:=wdDoNotSaveChanges

Next i

End With
End Sub


HTH and have a great day!

Steve Lang
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top