Option Explicit
Public Lecteur As String
'---------------------------------------------------------------------
'Avant utilisation
'Ajoute un Userform au projet
'Sur ce userform,
' place un contrôle ListView nommé ListView1
' place un contrôle CommandButton nommé CommandButton1
'---------------------------------------------------------------------
Private Sub ChoisirLecteur()
Dim maSelection As Object
On Error GoTo Fin
Set maSelection = ListView1.SelectedItem
If maSelection Is Nothing Then MsgBox "Aucune sélection!": Exit Sub
Lecteur = ListView1.SelectedItem
' enregistrement
ActiveWorkbook.SaveAs Filename:=Lecteur & ActiveWorkbook.Name
Fin:
Unload Me
End Sub
Private Sub CommandButton1_Click()
ChoisirLecteur
End Sub
Private Sub ListView1_DblClick()
ChoisirLecteur
End Sub
Private Sub UserForm_Initialize()
Dim Obj_fso As Object
Set Obj_fso = CreateObject("Scripting.FileSystemObject") '
Dim d 'As Drive
Dim tmp() As Variant, x As Integer, i As Integer, j As Variant
Dim tabTitreColonne As Variant
Dim tabLargColonne As Variant
Dim maStr As String
Dim Largeur As Integer
tabTitreColonne = Array("Lecteur", "Nom", "Type")
tabLargColonne = Array(50, 50, 50)
For Each d In Obj_fso.Drives
If d.IsReady Then
ReDim Preserve tmp(0 To 2, 0 To x)
tmp(0, x) = d.RootFolder 'Path
tmp(1, x) = d.VolumeName
Select Case d.DriveType
Case 0: maStr = "Inconnu"
Case 1: maStr = "Amovible "
Case 2: maStr = "Fixe"
Case 3: maStr = "Réseau"
Case 4: maStr = "CD -ROM"
Case 5: maStr = "Mémoire virtuelle"
End Select
tmp(2, x) = maStr
x = x + 1
End If
Next
With ListView1
.Top = 6
.Left = 6
For i = 0 To UBound(tabLargColonne)
Largeur = Largeur + tabLargColonne(i)
Next i
.Width = Largeur + 5
.Height = 82
'interdiction de selection multiple
.MultiSelect = False
'quadrillage
.Gridlines = True
'selection de ligne complete
.FullRowSelect = True
'modification colonne 1 interdite
.LabelEdit = 1
'Remplissage du ListView
For i = 0 To UBound(tmp, 2)
.ColumnHeaders.Add , tmp(0, i), tabTitreColonne(i), tabLargColonne(i)
.ListItems.Add , , tmp(0, i)
.ListItems(i + 1).ListSubItems.Add , , tmp(1, i)
.ListItems(i + 1).ListSubItems.Add , , tmp(2, i)
Next i
'premiere ligne nonsélectionnée
.ListItems(1).Selected = False
Set .SelectedItem = Nothing
'Spécifie l'affichage en mode "Détails"
.View = lvwReport
End With
Me.Width = 6 + ListView1.Width + 3 + 6
With CommandButton1
.Top = ListView1.Top + ListView1.Height + 3
.Width = 40
.Height = 20
.Left = Me.Width - .Width - 6 - 3
End With
Me.Height = 6 + 15 + ListView1.Height + 6 + CommandButton1.Height + 6
Me.Caption = "Choisissez le lecteur"
End Sub