1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
| '=================================================
' Dans les déclarations (tout en haut du code)
'=================================================
Dim aResultat() As String
Dim lRet As Long
'=================================================
' Code à mettre dans ton bouton Ouvrir
'=================================================
Private Sub BoutonOuvrir_Click()
' Exemple avec le bloc-note et un fichier texte
Shell "notepad" & Space(1) & aResultat(NomDeTaListbox.ListIndex), vbMaximizedFocus
' pense à remplacer le nom de la listbox ici ^^
End Sub
'=================================================
' Code à mettre dans ton bouton Lister
'=================================================
Private Sub BoutonLister_Click()
Dim i As Long
' Change ici le nom de la listbox
NomDeTaListbox.Clear
' Change ici la directory, tu auras des Select Case à faire
' Selon le choix dans la ComboBox
lRet = GetFilesPathFromDirectory("C:\Windows\", aResultat(), "*.*")
If lRet <> -1 Then
For i = 0 To lRet
' Change ici le nom de la listbox
NomDeTaListbox.AddItem GetFileNameFromPath(aResultat(i))
Next i
End If
End Sub
'=================================================
' Fonction à ne pas toucher
' et à placer parmis tes codes du UserForm
'=================================================
Private Function GetFilesPathFromDirectory(ByVal sDir As String, ByRef aRet() As String, Optional ByVal sFilter As String = "*.txt") As Long
' GetFilesPathFromDirectory retourne -1 si aucun fichier trouvé
' sinon retourne la dimension du tableau, donc NB fichiers -1 (tableau commence à l'indice 0)
' init les résultats
GetFilesPathFromDirectory = -1
Erase aRet
If RightB$(sDir, 2) <> "\" Then sDir = sDir & "\"
' formate le chemin
Dim sFile As String, lIndex As Long
sFile = Dir(sDir & sFilter, vbHidden Or vbSystem)
' redimentionne après vérif premier fichier
If sFile <> vbNullString Then
lIndex = 0
ReDim aRet(lIndex)
aRet(lIndex) = sDir & sFile
sFile = Dir
' boucle sur tous les fichiers
Do While sFile <> vbNullString
lIndex = UBound(aRet) + 1
ReDim Preserve aRet(lIndex)
aRet(lIndex) = sDir & sFile
sFile = Dir
Loop
' retour
GetFilesPathFromDirectory = lIndex
End If
End Function
Private Function GetFileNameFromPath(ByVal sPath As String)
GetFileNameFromPath = Right$(sPath, Len(sPath) - InStrRev(sPath, "\"))
End Function |
Partager