Path of files

I

Ivica Lopar

This code put all "excel" files in ListBox1 from folder who start with name
in TextBox1.
Can I change cod that he put all files from any folder(folder and files <>
excel file).


Private Sub CommandButton1_Click()
Dim objFSO As Object
Dim objFolder As Object
Dim objFile As Object

Set objFSO = CreateObject("Scripting.FileSystemObject")
On Error Resume Next
Set objFolder = objFSO.GetFolder(Me.TextBox1.Text)
On Error GoTo 0
If Not objFolder Is Nothing Then
For Each objFile In objFolder.Files
If objFile.Type = "Microsoft Excel Worksheet" Then
Me.ListBox1.AddItem objFile.Name
End If
Next
End If
End Sub



Regards
Lopar
 
D

Dave Peterson

Maybe...

Option Explicit
Private Sub CommandButton1_Click()
Dim objFSO As Object
Dim objFolder As Object
Dim objFile As Object
Dim objSubFolder As Object

Set objFSO = CreateObject("Scripting.FileSystemObject")
On Error Resume Next
Set objFolder = objFSO.GetFolder(Me.TextBox1.Text)
On Error GoTo 0
If Not objFolder Is Nothing Then
For Each objFile In objFolder.Files
'If objFile.Type = "Microsoft Excel Worksheet" Then
Me.ListBox1.AddItem objFile.Name
'End If
Next objFile
For Each objSubFolder In objFolder.subFolders
Me.ListBox1.AddItem objSubFolder.Name
Next objSubFolder
End If
End Sub

If you want to exclude the excel worksheets (I wasn't sure):
change this:

'If objFile.Type = "Microsoft Excel Worksheet" Then
Me.ListBox1.AddItem objFile.Name
'End If
to
If objFile.Type <> "Microsoft Excel Worksheet" Then
Me.ListBox1.AddItem objFile.Name
End If

(Uncommented and = changed to <>.)
 

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