test fichier ouvert

shadowman_2k3

Nouveau membre
Salut à tous,

J'ai une fonction copier/couper en vba et pour la partie couper, j'ai besoin de tester si le fichier est ouvert.
Voici le code
[cpp]'The function copy_files carries out a copy/cut of a tree structure of files in only one folder, and it sends the number of file it copies/cuts.
'srcfolder is the folder of the source of the tree structure.
'destfolder is the destination's folder.
'subfolders is a boolean who allows the transaction in the sub folders of the source folder.
'suppr is a bolean who is allows the cut of the transaction
Function copy_files(srcFolder As String, destFolder As String, subFolders As Boolean, suppr As Boolean) As Integer

On Error GoTo errHandler

Dim fso As New FileSystemObject
Dim theFile As file
Dim theFiles As Files
Dim theFolder As Folder
Dim theSubFolders As Folders

Set theFolder = fso.GetFolder(srcFolder)
Set theFiles = theFolder.Files
Set theSubFolders = theFolder.subFolders

If theFiles.Count > 0 Then

copy_files = theFiles.Count
For Each theFile In theFiles
If suppr Then
fso.MoveFile theFile, destFolder & "\"
Else
fso.CopyFile theFile, destFolder & "\"
End If
Next

End If

Set theFile = Nothing
Set theFiles = Nothing
Set theFolder = Nothing
Set fso = Nothing

If theSubFolders.Count > 0 And subFolders Then

For Each theFolder In theSubFolders
If suppr Then
copy_files = copy_files + copy_files(theFolder.path, destFolder, True, True)
Else
copy_files = copy_files + copy_files(theFolder.path, destFolder, True, False)
End If
Next

End If

Set theSubFolders = Nothing

Exit Function

If Not Error Then
GoTo fin
End If

errHandler:

copy_files = -err.Number

fin:

End Function
[/cpp]

Donc comment faire pour tester le fichier?


Merci
 

shadowman_2k3

Nouveau membre
jlé regardé ya 2h avec la tête dans le *** et je trouvait que ca répondait pas à mon porblème, mais après le repas de suite ca marche mieux ^^

merci pour le coup de lanterne :)
 

shadowman_2k3

Nouveau membre
par contre j'ai un pb avec le code, il veut pas compiler, voici le nouveau code :
[cpp]
'The function copy_files carries out a copy/cut of a tree structure of files in only one folder, and it sends the number of file it copies/cuts.
'srcfolder is the folder of the source of the tree structure.
'destfolder is the destination's folder.
'subfolders is a boolean who allows the transaction in the sub folders of the source folder.
'suppr is a bolean who is allows the cut of the transaction
Function copy_files(srcFolder As String, destFolder As String, subFolders As Boolean, suppr As Boolean) As Integer

On Error GoTo errHandler

Dim fso As New FileSystemObject
Dim theFile As file
Dim theFiles As Files
Dim theFolder As Folder
Dim theSubFolders As Folders
Dim n

Set theFolder = fso.GetFolder(srcFolder)
Set theFiles = theFolder.Files
Set theSubFolders = theFolder.subFolders

If theFiles.Count > 0 Then

copy_files = theFiles.Count
For Each theFile In theFiles
If suppr Then
n = fso.GetFileName(theFile)
If IsFileOpen(n) Then
MsgBox "fichier en cours d'utilisation"
Else
fso.MoveFile theFile, destFolder & "\"
End If
Else
fso.CopyFile theFile, destFolder & "\"
End If
Next

End If

Set theFile = Nothing
Set theFiles = Nothing
Set theFolder = Nothing
Set fso = Nothing

If theSubFolders.Count > 0 And subFolders Then

For Each theFolder In theSubFolders
If suppr Then
copy_files = copy_files + copy_files(theFolder.path, destFolder, True, True)
Else
copy_files = copy_files + copy_files(theFolder.path, destFolder, True, False)
End If
Next

End If

Set theSubFolders = Nothing

Exit Function

If Not Error Then
GoTo fin
End If

errHandler:

copy_files = -err.Number

fin:

End Function
[/cpp]
 

shadowman_2k3

Nouveau membre
si c'était créer mais ya le ierr qui passe pas, donc je l'ai mis en commentaire et puis tampis pour la fonction donc tampis :(
 
Vous devez vous inscrire ou vous connecter pour répondre ici.
Derniers messages publiés
Statistiques globales
Discussions
730 079
Messages
6 716 715
Membres
1 586 247
Dernier membre
MrAzgarIII
Partager cette page
Haut