Private Sub UserForm_Initialize()
ListBox1.ColumnCount = 2 'nombre de colonne de la listbox
ListBox1.ColumnWidths = "200;0" 'largeur des colonnes, la 2ème = 0
ListBoxOK "*.pdf", "C:\Users\a.chataignereau\Documents\Collègues\Commerciaux\Abdul"
End Sub
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Cancel = Not ListBoxOK(TextBox1.Text, "C:\Users\a.chataignereau\Documents\Collègues\Commerciaux\Abdul")
If Cancel Then TextBox1.Text = ""
End Sub
Private Function ListBoxOK(ByVal Masque As String, ByVal Chemin As String) As Boolean
If InStr(Masque, "*") = 0 Then Masque = "*" & Masque & "*.pdf"
Dim FSO As Scripting.FileSystemObject, Rep As Scripting.Folder
Dim TLBx(), L As Long
If Masque <> "" Then
Set FSO = New Scripting.FileSystemObject
Set Rep = FSO.GetFolder(Chemin)
L = NbFich(Masque, Rep)
ListBoxOK = L > 0
If ListBoxOK Then
ReDim TLBx(1 To L, 1 To 2)
L = 0
Fichiers Masque, TLBx, L, Rep
ListBox1.List = TLBx
Else
MsgBox "Aucun fichier """ & Masque & """ trouvé.", vbInformation, "Pas de fichier."
End If
End If
End Function
Private Function NbFich(ByVal Masque As String, ByVal Dos As Scripting.Folder) As Long
Dim F As Scripting.File, SDos As Scripting.Folder
On Error Resume Next
For Each F In Dos.Files
If F.Name Like Masque Then NbFich = NbFich + 1
Next F
For Each SDos In Dos.SubFolders
NbFich = NbFich + NbFich(Nom, SDos)
Next SDos
End Function
Private Sub Fichiers(ByVal Masque As String, TLBx(), L As Long, ByVal Dos As Scripting.Folder)
Dim F As Scripting.File, SDos As Scripting.Folder
On Error Resume Next
For Each F In Dos.Files
If F.Name Like Masque Then
L = L + 1
TLBx(L, 1) = F.Name
TLBx(L, 2) = F.Path
End If
Next F
For Each SDos In Dos.SubFolders
Fichiers Nom, TLBx, L, SDos
Next SDos
End Sub