| Sous XL2000 et Win XP
| Désolé, seuls les répertoires et ssrépertoires sont créés sur ma machine
Si tel est le cas, c'est étrange, je n'éprouve aucune difficulté sous Windows7 et Excel 2007
Je te remercie pour les tests que tu as effectués.
Si tu préfères l'usage des API de Windows....
La copie est quand même assez longue compte tenu que
la copie s'effectue vers un clé... Tu peux remplacer tout le
code du module du classeur exemple avec ceci :
'Déclaration des API dans le haut d'un module Standard
Type SHFILEOPSTRUCT
hwnd As Long
wFunc As Long
pFrom As String
pTo As String
fFlags As Integer
fAnyOperationsAborted As Boolean
hNameMappings As Long
lpszProgressTitle As String
End Type
Const FO_MOVE = 1
Const FO_COPY = 2
Const FO_DELETE = 3
Const FO_RENAME = 4
Const FOF_SILENT = 4
Const FOF_NOCONFIRMATION = 10
Private Declare Function SHFileOperationA Lib "Shell32.dll" _
(lpFileOp As SHFILEOPSTRUCT) As Long
'-------------------------------------
Sub Test()
Dim LecteurSource As String, Prêt As Boolean
Dim LecteurDestination As String
Dim Chemin As String
Dim Répertoire As String
'Le répertoire où se retrouve le fichier
Répertoire = ThisWorkbook.Path & ""
'Si on désire effectuer la copie vers la clé vers un
'répertoire d'appellation différente que le répertoire sourcee :
'*************** À RENSEIGNER******************
Chemin = "toto\tutu\titi" ' à renseigner
'***********************************************
LecteurSource = Left(Répertoire, 3)
If Chemin = "" Then
Chemin = Right(Répertoire, Len(Répertoire) - 3)
End If
Application.Cursor = xlWait
'Trouve le lecteur de la clé.
LecteurDestination = RemovableDisk(LecteurSource)
If LecteurDestination <> "" Then
'Teste si le lecteur est prêt...
Prêt = EstPret(LecteurDestination)
If Prêt = False Then
MsgBox "Le lecteur amovible n'est pas prêt."
Exit Sub
End If
Else
MsgBox "Aucun disque amovible détecté."
Exit Sub
End If
'Création du répertoire si nécessaire sur la clé
'si le répertoire est déjà présent, rien n'est modifié.
Creer_Chemin LecteurDestination, Chemin
DoEvents
'Copier en utiliser les API de Windows du répertoire source
'vers le répertoire de destination
If CopieDossier(Répertoire, LecteurDestination & Chemin, FO_COPY, True) Then
MsgBox "Opération réussie"
Else
MsgBox "Un problème fut rencontré."
End If
Application.Cursor = xlDefault
End Sub
'-------------------------------------
Function RemovableDisk(MonLecteur As String)
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\" & _
strComputer & "\root\cimv2")
Set colDisks = objWMIService.ExecQuery _
("Select * from Win32_LogicalDisk")
For Each Objdisk In colDisks
'2 constante numérique pour disque dur "removable"
If Objdisk.DriveType = 2 Then
RemovableDisk = Objdisk.Name & ""
Exit Function
End If
Next
End Function
'-------------------------------------
Function EstPret(Lecteur As String)
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set colDrives = objFSO.Drives
For Each objdrive In colDrives
If Lecteur = objdrive & "" Then
If objdrive.IsReady = True Then
EstPret = objdrive.IsReady
End If
End If
Next
End Function
'-------------------------------------
Sub Creer_Chemin(Racine, Chemin As String)
Dim Commande As String
'Crée le chemin indiqué si les répertoires ne
'sont pas présents. N'écrase rien !
ChDrive Racine
Commande = Environ("comspec") & " /c mkdir " & Chemin
Shell Commande, 0
End Sub
'-------------------------------------
Private Function CopieDossier(Source As String, Dest As String, _
Optional Action As Byte, Optional Animation As Boolean) As Boolean
Dim OpStruct As SHFILEOPSTRUCT
With OpStruct
.wFunc = Action
.pFrom = Source
.pTo = Dest
'POUR MESSAGE AVANT D'ÉCRASER : 4 AU LIEU DE 10
.fFlags = 4
End With
CopieDossier = IIf(SHFileOperationA(OpStruct), False, True)
End Function
'-------------------------------------
MichD