PDA

View Full Version : Listbox - List folders/subfolders+files



jmaocubo
08-18-2011, 08:34 AM
Hi,

I need help: pray2:

MS Word 2007

I have a userform that contains three listboxes.

What I need is the listbox1 to list all the folders of the current patch where the word file is;

In ListBox2 list all folders in selected folder listbox1;

In listbox3 list all *. doc files containing the folder selected in ListBox2.

Thanks in advance

Miguel

Frosty
08-18-2011, 12:25 PM
Do some searches on FileSystemObject, and you should be able to get what you're looking for, if you really need a custom form.

Sounds like you're trying to recreate a Mac Finder window :)

However, if you don't want to recreate some kind of file search dialog, you might look into re-purposing the Dialogs(wdDialogFileOpen) object, or using the FileDialog object. Three examples (1 of each) below (all have been posted on this site before).

Public Sub OpenADocument(Optional sSuggestedPath As String)
With Dialogs(wdDialogFileOpen)
'if you don't provide the variable, it will use
.Name = sSuggestedPath
'use this to display the dialog
.Show
'can use this to automatically open the file of the suggested path
'.Execute
End With
End Sub
'Return a valid path from a dialog, or return an empty string
Public Function fGetPathViaDialog(Optional bShowAMessageBox As Boolean = True) As String
Dim sRet As String
Dim oMyDialog As FileDialog

'set our dialog to...
Set oMyDialog = Application.FileDialog(msoFileDialogFolderPicker)

'set some of the available custom properties (use locals window to see more)
With oMyDialog
'OK button
.ButtonName = "Hello"
'caption (top of the dialog)
.Title = "Find my stuff"
'if you allow this, the selected items may have more than 1
.AllowMultiSelect = False
.InitialFileName = "C:\Users\"

'show it
.Show
'return the string (notice that it takes off the end "\"), if the user didn't hit cancel
If .SelectedItems.Count > 0 Then
sRet = .SelectedItems(1)
End If
End With

'messagebox it?
If bShowAMessageBox Then
If sRet = "" Then
MsgBox "You hit cancel!"
Else
MsgBox "You chose:" & vbCr & sRet
End If
End If

'and don't forget to actually have the function return the value
fGetPathViaDialog = sRet
End Function
Sub DealingWithFSO()
'***Our FSO objects, to allow us to iterate through our main folder
Dim oFSO As Object
Dim oMainFolder As Object
Dim oFolder As Object
Dim oFile As Object

'get an instance of the FileSystemObject
Set oFSO = CreateObject("Scripting.FileSystemObject")
'create a reference to the main folder
Set oMainFolder = oFSO.GetFolder("C:\Users")

For Each oFolder In oMainFolder.SubFolders
Debug.Print oFolder.Name
For Each oFile In oFolder.Files
Debug.Print oFile.Name
Next
Next

End Sub