UtterAccess.com
X   Site Message
(Message will auto close in 2 seconds)

Welcome to UtterAccess! Please ( Login   or   Register )

Custom Search
 
   Reply to this topicStart new topic
> Populate A Listbox With Files From A Directory, Any Version    
 
   
dallr
post Jul 5 2006, 09:51 AM
Post#1


UtterAccess VIP
Posts: 3,081
Joined: 4-November 04
From: The Land of the Access Ninja's


Hi Guys just trying to add to the archives again.
The following code pulls all the files from a directory and displays it in a listbox then allows the user to double click on the file in the list box to open it.
The user can also filter the function to only show the files that they want in the listbox. You filter by including the file extension (eg. xls, doc, txt)in the PopListBox Function. Otherwise leave it out to show all the files.
To populate your listbox you can use the forms ON OPEN event and include the following.
CODE

With Me.MyListBox    'the name of your listbox  
    .RowSource = PopListBox("c:\",'txt")   ' You can leave the ext. out to show all files  
    .RowSourceType = "value list"
End With

To allow you to double click and open the file from the listbox. In the listbox DOUBLE CLICK event place the following code.
CODE

'Form1=Name of your form the listbox is on
'MyListBox = Name of your list box.
'C:\ =  The name of your directory. Note this must be the same as in the PopListBox Function.
    
On Error Resume Next
Application.FollowHyperlink OpenListItem("Form1", "MyListBox", "c:\")

These are the function you can include in one module.
CODE
  
Function PopListBox(strPath As String, Optional FileExtension As String) As String
Dim MyFso As Object
Dim MainDir As Object
Dim MyFile As Object
Dim LstItems As String, FileProp As String
Dim NameLength As Integer, InitialNameLength As Integer, TypeLength As Integer, InitialTypeLength As Integer
Dim x As Integer, y As Integer
Dim NameSep As Integer, TypeSep As Integer
Dim Placer As Variant
On Error Resume Next
Set MyFso = CreateObject("scripting.FileSystemObject")
Set MainDir = MyFso.GetFolder(strPath)
InitialNameLength = 1
InitialTypeLength = 1
For Each MyFile In MainDir.Files ' all this in side this loop is to help try and format the columns a bit more uniformly.
        NameLength = Len(MyFile.Name)
        x = IIf(NameLength > InitialNameLength, NameLength, InitialNameLength)
        InitialNameLength = x
        
        TypeLength = Len(MyFile.Type)
        y = IIf(TypeLength > InitialTypeLength, TypeLength, InitialTypeLength)
        InitialTypeLength = y
Next
For Each MyFile In MainDir.Files
    Placer = InStr(1, MyFile.Name, ".") - 1
    NameSep = x - Len(MyFile.Name) + 4
    TypeSep = y - Len(MyFile.Type) + 4
    
    If Mid(MyFile.Name, InStr(1, MyFile.Name, ".") + 1) = FileExtension Then
        FileProp = Mid(MyFile.Name, 1, Placer) & String(NameSep, "_") & MyFile.Type & String(TypeSep, "_") & MyFile.DateCreated
        LstItems = LstItems & FileProp & ";"
    ElseIf FileExtension = "" Then
        FileProp = Mid(MyFile.Name, 1, Placer) & String(NameSep, "_") & MyFile.Type & String(TypeSep, "_") & MyFile.DateCreated
        LstItems = LstItems & FileProp & ";"
    End If
    
Next
Debug.Print LstItems
'Clean up
Set MyFso = Nothing
Set MainDir = Nothing
PopListBox = UCase(LstItems)
End Function

CODE

Function OpenListItem(FormName As String, ControlName As String, strPath As String) As String
Dim frm As Form
Dim ctl As Control
Dim intI As Integer
Dim MyVar As Variant
Dim strFileName As String
Dim MyFso As Object
Dim MainDir As Object
Dim MyFile As Object
On Error Resume Next
Set MyFso = CreateObject("scripting.FileSystemObject")
Set MainDir = MyFso.GetFolder(strPath)
Set frm = Forms(FormName)
Set ctl = frm.Controls(ControlName)
For Each MyVar In ctl.ItemsSelected
    strFileName = ctl.ItemData(MyVar)
    strFileName = Mid(strFileName, 1, InStr(1, strFileName, "_") - 1)
    
    For Each MyFile In MainDir.Files
        If Mid(MyFile.Name, 1, InStr(1, MyFile.Name, ".") - 1) = strFileName Then
            OpenListItem = MyFile.Path
            Exit For
        End If
    Next
Next
End Function

Hope you find it helpful. I have also upload a DB for those who want to download.
Dallr
Attached File(s)
Attached File  Populate a Listbox with Files from a Directory.zip ( 22.42K )Number of downloads: 1192
 
Go to the top of the page
 
fitzdesignz
post Apr 15 2019, 04:33 PM
Post#2



Posts: 74
Joined: 14-June 18



I cant get this to open excel files.. Any ideas?

--------------------
-Fitz
Go to the top of the page
 
MadPiet
post Apr 15 2019, 04:42 PM
Post#3



Posts: 3,368
Joined: 27-February 09



The code is populating a listbox with filenames. (And it's crazy old. Might need to blow some dust off it.) What are you trying to do really?

If you just want to specify a directory and populate a listbox with the contents of it, it should be pretty trivial.
Go to the top of the page
 
fitzdesignz
post Apr 16 2019, 09:30 AM
Post#4



Posts: 74
Joined: 14-June 18



The code populates a list box with files from a directory. When you double click the list box item it opens that file. It works for PDF's and word documents but I cannot get it to open excel files.

--------------------
-Fitz
Go to the top of the page
 
fitzdesignz
post Apr 16 2019, 01:56 PM
Post#5



Posts: 74
Joined: 14-June 18



Okay I found the issue. My file name had an underscore in it. Removing the _ fixed the issue.
This post has been edited by fitzdesignz: Apr 16 2019, 01:59 PM

--------------------
-Fitz
Go to the top of the page
 


Custom Search


RSSSearch   Top   Lo-Fi    15th December 2019 - 11:42 PM