Populate combobox with PDF filenames from a directory (1 Viewer)

gojets1721

Registered User.
Local time
Today, 13:22
Joined
Jun 11, 2019
Messages
430
I am trying to populate the values of a combo box with all PDF files in a particular windows folder.

I don't need it to open the file or anything when selected in the combo; just simply populate the the values with the current PDF files in a specific directory.

Any suggestions?
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 16:22
Joined
May 21, 2018
Messages
8,605
Pass in the path and this returns a collection. You can loop that collection and use the combos addItem method to load it.
Code:
Public Function AllFiles(ByVal FullPath As String) _
  As Collection
'***************************************************
'PURPOSE: Returns all files in a folder using
'the FileSystemObject

'PARAMETER: FullPath = FullPath to folder for
'which you want all files

'************************************************

Dim oFs As New FileSystemObject
Dim sAns As New Collection
Dim oFolder As scripting.Folder
Dim oFile As scripting.file
Dim lElement As Long

If oFs.FolderExists(FullPath) Then
    
    Set oFolder = oFs.GetFolder(FullPath)
 
    For Each oFile In oFolder.Files
      If Right(oFile.Name, 4) = ".PDF" Then
        sAns.Add oFile.Name
      End If
   Next
Else
  MsgBox FullPath & "  Not found in load imagelist.", vbInformation
End If
'Debug.Print sAns.Count
Set AllFiles = sAns
ErrHandler:
    Set oFs = Nothing
    Set oFolder = Nothing
    Set oFile = Nothing
End Function
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 16:22
Joined
May 21, 2018
Messages
8,605
Untested but something like

Code:
dim files as Collection
dim i as integer
set files = AllFiles("C:SomeFolder\SomeSubfolder")
for i = 1 to files.count
  me.cmbo1.addItem files(i)
next i
 

gojets1721

Registered User.
Local time
Today, 13:22
Joined
Jun 11, 2019
Messages
430
Thank you. How do I use this with my particular combo box?
 

gojets1721

Registered User.
Local time
Today, 13:22
Joined
Jun 11, 2019
Messages
430
Untested but something like

Code:
dim files as Collection
dim i as integer
set files = AllFiles("C:SomeFolder\SomeSubfolder")
for i = 1 to files.count
  me.cmbo1.addItem files(i)
next i
Where do I place that code?
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 16:22
Joined
May 21, 2018
Messages
8,605
One thing about the additem method versus using a rowsource with a string. You cannot easily change the list without removing the items using the removeitem. Probably not going to be a problem, unless you need to refresh the list once you already loaded it. If you have to refresh and rerun the load you need to first loop the items in the combo box and remove then, then add the new list.
 

Users who are viewing this thread

Top Bottom