1 pièce(s) jointe(s)
FilePicker et FolderPicker pour UserForm
Bonjour à tous,
Ce post sert à présenter un module de classe permettant de faciliter la manipulation des FilePickers/FolderPickers sur un formulaire.
Ce module de classe Cls_FrmTool_FileFolderPicker sert à lier 3 contrôles :
- Un Label contenant le Caption du FilePicker/FolderPicker
- Un Label contenant la Value du FilePicker/FolderPicker
- Un CommandButton permettant d’ouvrir la fenêtre de sélection du fichier/dossier
La sélection du fichier/dossier peut se faire de deux manières :
- Via un clic sur le CommandButton
- Via un double-clic sur le Label contenant la Value du FilePicker/FolderPicker
La classe Cls_FrmTool_FileFolderPicker contient :
- 2 méthodes permettant d’initialiser le contrôle (InitFilePicker et InitFolderPicker)
- Les propriétés suivantes en lecture/écriture permettant d’interagir avec le contrôle :
- Value
- Caption
- Enabled
- Visible
- FilterDescr
- FilterExt
- Title
L’exemple ci-joint montre comment mettre en œuvre ce module de classe pour gérer plusieurs FilePickers/FolderPickers au sein d’un même formulaire et comment interagir avec eux.
Code du module de classe Cls_FrmTool_FileFolderPicker :
Code:
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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
| '#######################################################################################
'
' Module : Cls_FrmTool_FileFolderPicker
' Auteur : Mickaël
' Date : 14/09/2020
' Description : Module de code dédié à gérer les FilePicker et FolderPicker d'un formulaire.
' Dans la conception d'un formulaire, un FilePicker ou un FolderPicker est composé
' d'un ensemble de 3 contrôle :
' - un Label contenant la description du champ de saisie
' - un Label contenant l'emplacement du path (la "valeur" du FilePicker/FolderPicker)
' - un CommandButton pour afficher la boite de sélection du fichier/dossier
' Cette classe permet de "lier ces contrôles" et de faciliter leur manipulation.
' Le clic sur le bouton ou le double-clic sur le "label valeur" permet d'ouvrir la boite de
' sélection du fichier/dossier.
' Interfaces :
' - Méthode InitFilePicker
' Méthode dédiée à initialiser un FilePicker.
' Paramètres:
' > p_o_lblText contrôle Label contenant la description du FilePicker
' > p_o_lblValue contrôle Label contenant l'emplacement du path du FilePicker
' > p_o_btn contrôle CommandButton du FilePicker
' > (p_s_filterDescr) OPTIONNEL - permet (avec p_s_filterExt) de définir un filtre de fichier
' > (p_s_filterExt) OPTIONNEL - permet (avec p_s_filterDescr) de définir un filtre de fichier
' > (p_s_dialogBoxTitle) OPTIONNEL - permet de définir le titre de la boite de sélection de fichier
' > (p_s_initialFilePath) OPTIONNEL - permet de définir une valeur de base au FilePicker
' - Méthode InitFolderPicker
' Méthode dédiée à initialiser un FolderPicker.
' Paramètres:
' > p_o_lblText contrôle Label contenant la description du FolderPicker
' > p_o_lblValue contrôle Label contenant l'emplacement du path du FolderPicker
' > p_o_btn contrôle CommandButton du FolderPicker
' > (p_s_dialogBoxTitle) OPTIONNEL - permet de définir le titre de la boite de sélection de dossier
' > (p_s_initialFolderPath) OPTIONNEL - permet de définir une valeur de base au FolderPicker
' - Propriété Value (String) - lecture/écriture : permet de définir/surcharger la valeur (chemin) du FilePicker/FolderPicker
' - Propriété Caption (String) - lecture/écriture : permet de définir/surcharger la description IHM du FilePicker/FolderPicker
' - Propriété Enabled (Boolean) - lecture/écriture : permet de définir l'accessibilité du FilePicker/FolderPicker
' - Propriété Visible (Boolean) - lecture/écriture : permet de définir la visibilité du FilePicker/FolderPicker
' - Propriété FilterDescr (String) - lecture/écriture : permet de définir/surcharger la description du filtre du FilePicker
' - Propriété FilterExt (String) - lecture/écriture : permet de définir/surcharger l'extension du filtre du FilePicker
' - Propriété Title (String) - lecture/écriture : permet de définir/surcharger le titre de la boite de sélection de fichier/dossier
'
'#######################################################################################
Option Explicit
Private WithEvents m_o_lblValue As MSForms.Label
Private m_o_lblText As MSForms.Label
Private WithEvents m_o_btnFile As MSForms.CommandButton
Private WithEvents m_o_btnFolder As MSForms.CommandButton
Private m_s_filterDescr As String
Private m_s_filterExt As String
Private m_s_title As String
Private Property Get MyFSO() As Object 'Scripting.FileSystemObject
Static s_o_fso As Object 'Scripting.FileSystemObject
If s_o_fso Is Nothing Then Set s_o_fso = CreateObject("Scripting.FileSystemObject")
Set MyFSO = s_o_fso
End Property
Public Sub InitFilePicker(p_o_lblText As MSForms.Label, p_o_lblValue As MSForms.Label, p_o_btn As MSForms.CommandButton, Optional p_s_filterDescr As String = "Tous les fichiers", Optional p_s_filterExt As String = "*.*", Optional p_s_dialogBoxTitle As String = "Sélection du fichier", Optional p_s_initialFilePath As String = vbNullString)
Dim l_o_ctrl As MSForms.Control
Set m_o_lblText = p_o_lblText
Set m_o_lblValue = p_o_lblValue
Set m_o_btnFile = p_o_btn
m_s_filterDescr = p_s_filterDescr
m_s_filterExt = p_s_filterExt
m_s_title = p_s_dialogBoxTitle
SetPath p_s_initialFilePath
End Sub
Public Sub InitFolderPicker(p_o_lblText As MSForms.Label, p_o_lblValue As MSForms.Label, p_o_btn As MSForms.CommandButton, Optional p_s_dialogBoxTitle As String = "Sélection du dossier", Optional p_s_initialFolderPath As String = vbNullString)
Set m_o_lblText = p_o_lblText
Set m_o_lblValue = p_o_lblValue
Set m_o_btnFolder = p_o_btn
m_s_title = p_s_dialogBoxTitle
SetPath p_s_initialFolderPath
End Sub
Public Property Let Value(p_s_path As String)
SetPath p_s_path
End Property
Public Property Get Value() As String
Value = Strings.Trim(m_o_lblValue.Caption)
End Property
Public Property Get Caption() As String
Caption = m_o_lblText.Caption
End Property
Public Property Let Caption(p_s_value As String)
m_o_lblText.Caption = p_s_value
End Property
Public Property Get Enabled() As Boolean
Dim l_o_btn As MSForms.CommandButton
If m_o_btnFile Is Nothing Then Set l_o_btn = m_o_btnFolder Else Set l_o_btn = m_o_btnFile
Enabled = l_o_btn.Enabled
Set l_o_btn = Nothing
End Property
Public Property Let Enabled(p_b_value As Boolean)
Dim l_o_btn As MSForms.CommandButton
If m_o_btnFile Is Nothing Then Set l_o_btn = m_o_btnFolder Else Set l_o_btn = m_o_btnFile
m_o_lblValue.Enabled = p_b_value
l_o_btn.Enabled = p_b_value
Set l_o_btn = Nothing
End Property
Public Property Get FilterDescr() As String
FilterDescr = m_s_filterDescr
End Property
Public Property Let FilterDescr(p_s_value As String)
m_s_filterDescr = p_s_value
End Property
Public Property Get FilterExt() As String
FilterExt = m_s_filterExt
End Property
Public Property Let FilterExt(p_s_value As String)
m_s_filterExt = p_s_value
End Property
Public Property Get Title() As String
Title = m_s_title
End Property
Public Property Let Title(p_s_value As String)
m_s_title = p_s_value
End Property
Public Property Get Visible() As Boolean
Dim l_o_btn As MSForms.CommandButton
If m_o_btnFile Is Nothing Then Set l_o_btn = m_o_btnFolder Else Set l_o_btn = m_o_btnFile
Visible = l_o_btn.Visible
Set l_o_btn = Nothing
End Property
Public Property Let Visible(p_b_value As Boolean)
Dim l_o_ctrl As MSForms.Control
Dim l_o_btn As MSForms.CommandButton
If m_o_btnFile Is Nothing Then Set l_o_btn = m_o_btnFolder Else Set l_o_btn = m_o_btnFile
Set l_o_ctrl = m_o_lblText: l_o_ctrl.Visible = p_b_value
Set l_o_ctrl = m_o_lblValue: l_o_ctrl.Visible = p_b_value
Set l_o_ctrl = l_o_btn: l_o_ctrl.Visible = p_b_value
Set l_o_ctrl = Nothing
Set l_o_btn = Nothing
End Property
Private Function SetPath(p_s_path As String)
If Not m_o_btnFile Is Nothing Then
If MyFSO.FileExists(p_s_path) Then m_o_lblValue.Caption = " " & p_s_path Else m_o_lblValue.Caption = vbNullString
ElseIf Not m_o_btnFolder Is Nothing Then
If MyFSO.FolderExists(p_s_path) Then m_o_lblValue.Caption = " " & p_s_path Else m_o_lblValue.Caption = vbNullString
End If
End Function
Private Sub m_o_btnFile_Click()
SelectItem
End Sub
Private Sub m_o_btnFolder_Click()
SelectItem
End Sub
Private Sub SelectItem()
If Not m_o_btnFile Is Nothing Then
With Application.FileDialog(msoFileDialogFilePicker)
.AllowMultiSelect = False
.Title = m_s_title
.Filters.Clear
.Filters.Add m_s_filterDescr, m_s_filterExt
.Show
If .SelectedItems.Count > 0 Then SetPath .SelectedItems(1)
End With
ElseIf Not m_o_btnFolder Is Nothing Then
With Application.FileDialog(msoFileDialogFolderPicker)
.AllowMultiSelect = False
.Title = m_s_title
.Show
If .SelectedItems.Count > 0 Then SetPath .SelectedItems(1)
End With
End If
End Sub
Private Sub m_o_lblValue_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
SelectItem
End Sub |
Code du formulaire de test :
Code:
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
| Option Explicit
'propriété servant à gérer l'ensemble des FilePickers/FolderPickers
Private Property Get FileFolderPickers() As Collection
Static s_o_coll As VBA.Collection
If s_o_coll Is Nothing Then Set s_o_coll = New VBA.Collection
Set FileFolderPickers = s_o_coll
End Property
'procédure dédiée à initialiser les FilePickers/FolderPickers
Private Sub InitFileFolderPickers()
Dim l_o_fp As Cls_FrmTool_FileFolderPicker
Set l_o_fp = New Cls_FrmTool_FileFolderPicker: l_o_fp.InitFilePicker Me.Lbl_FileText_Excel, Me.Lbl_FilePath_Excel, Me.Btn_SelectFile_Excel, "Fichiers Excel", "*.xls*", "Sélection du fichier Excel", ThisWorkbook.FullName
FileFolderPickers.Add l_o_fp, "ExcelFile"
Set l_o_fp = New Cls_FrmTool_FileFolderPicker: l_o_fp.InitFilePicker Me.Lbl_FileText_Any, Me.Lbl_FilePath_Any, Me.Btn_SelectFile_Any
FileFolderPickers.Add l_o_fp, "AnyFile"
Set l_o_fp = New Cls_FrmTool_FileFolderPicker: l_o_fp.InitFolderPicker Me.Lbl_FolderText_Fold1, Me.Lbl_FolderPath_Fold1, Me.Btn_SelectFolder_Fold1, "Sélection du dossier de test"
FileFolderPickers.Add l_o_fp, "Folder1"
Set l_o_fp = New Cls_FrmTool_FileFolderPicker: l_o_fp.InitFolderPicker Me.Lbl_FolderText_Fold2, Me.Lbl_FolderPath_Fold2, Me.Btn_SelectFolder_Fold2
FileFolderPickers.Add l_o_fp, "Folder2"
Set l_o_fp = New Cls_FrmTool_FileFolderPicker: l_o_fp.InitFolderPicker Me.Lbl_FolderText_Fold3, Me.Lbl_FolderPath_Fold3, Me.Btn_SelectFolder_Fold3
FileFolderPickers.Add l_o_fp, "Folder3"
End Sub
'initialiser les FilePickers/FolderPickers et afficher le formulaire
Public Sub ShowForm()
InitFileFolderPickers
Me.Show
End Sub
Private Sub Btn_ChangeCaption_Fold3_Click()
FileFolderPickers("Folder3").Caption = IIf(FileFolderPickers("Folder3").Caption = "Dossier important !!", "Dossier", "Dossier important !!")
End Sub
Private Sub Btn_EnableDisable_Excel_Click()
FileFolderPickers("ExcelFile").Enabled = Not FileFolderPickers("ExcelFile").Enabled
End Sub
Private Sub Btn_ShowHide_Fold2_Click()
FileFolderPickers("Folder2").Visible = Not FileFolderPickers("Folder2").Visible
End Sub
Private Sub Btn_ShowValues_Click()
Dim l_s_msg As String
l_s_msg = "Liste des valeurs :"
l_s_msg = l_s_msg & vbNewLine & " > Fichier Excel : " & FileFolderPickers("ExcelFile").Value
l_s_msg = l_s_msg & vbNewLine & " > Fichier autre : " & FileFolderPickers("AnyFile").Value
l_s_msg = l_s_msg & vbNewLine & " > Dossier 1 : " & FileFolderPickers("Folder1").Value
l_s_msg = l_s_msg & vbNewLine & " > Dossier 2 : " & FileFolderPickers("Folder2").Value
l_s_msg = l_s_msg & vbNewLine & " > Dossier 3 : " & FileFolderPickers("Folder3").Value
MsgBox l_s_msg, vbInformation, "Info"
End Sub |
A+