Hi all
I'm trying to create a folder with process documents in it (mainly
xls,doc,ppt).
How can i get a list box to be populated with the contents of the folder and
then the file be opend when double clicked?
I know how to populate a list box from a query or table but not from a
windows folder.
All help is appreciated.
_____________
Naz
London
Add a List Box to your form.
Set it's RowSourceType property to Value List.
Leave it's record source property unbound.
Code the Form's Load event:
GetListFiles
Copy and Paste the below code into a Module:
Public Sub GetListFiles()
Dim MyName As String
Dim FS As Object
Dim I As Integer
Dim strList As String
On Error GoTo Err_Handler
Set FS = Application.FileSearch
With FS
.Lookin = "C:\MyPathAndFolderName\"
If .Execute > 0 Then
For I = 1 To .foundFiles.Count
MyName = Mid(.foundFiles(I), InStrRev(.foundFiles(I), "\") + 1)
strList = strList & MyName & ","
Next I
Else
MsgBox "There were no files found."
End If
End With
strList = Left(strList, Len(strList) - 1)
Forms!frmReportList!lstExternalFiles.RowSource = strList
Set FS = Nothing
Exit_This_Sub:
Exit Sub
Err_Handler:
MsgBox "Error #: " & Err.Number & " " & Err.Description
Resume Exit_This_Sub
End Sub
Change YourFormName and ListBoxName to whatever the actual name of
your form and list box is.
Code the ListBox Double-Click event:
Application.FollowHyperlink "c:\PathToFolder\FolderName\" &
Me.ListBoxName
I suggest the Double-click event to prevent the inadvertently opening
of a mistakenly selected file.