Bloqué sur Code VBA

jeanjeandada Messages postés 152 Date d'inscription lundi 21 avril 2008 Statut Membre Dernière intervention 20 novembre 2011 - 17 févr. 2009 à 14:23
bigfish_le vrai Messages postés 1835 Date d'inscription vendredi 13 mai 2005 Statut Membre Dernière intervention 20 novembre 2013 - 17 févr. 2009 à 16:36
Bonjour  forum,

Est-ce que qq1 de vous connaîtrez le bout de code VBA pour dire :
Pour tous les fichiers de tel dossier faire
1) lire le nom du fichier
2) Si une partie de ce nom comporte un "OK" alors ........

J'ai cela :
Set fs = CreateObject("Scripting.FileSystemObject")
Set rep = fs.getfoleder("repertoire_du_dossier")
For Each fileitem In rep
MsgBox (fileitem.Name)
Next

mais la ligne en vert ne fonctionne pas

Merci

4 réponses

hebus16 Messages postés 80 Date d'inscription vendredi 14 mai 2004 Statut Membre Dernière intervention 2 octobre 2009 1
17 févr. 2009 à 14:28
normal c'est Folder pas foleder
0
cs_Jack Messages postés 14006 Date d'inscription samedi 29 décembre 2001 Statut Modérateur Dernière intervention 28 août 2015 79
17 févr. 2009 à 15:14
+ il faut déclarer FileItem :
   Dim mFichier As File   '  'mFichier' à la place de FileItem
+ Ne donne pas à tes variables des noms qui pourraient ressember à des instructions ou objets, comme FileItem : Le compilateur pourrait avoir du mal à interpréter = bug

Pour ton test :
   If mFichier.Name Like "*OK*" Then ...
0
cs_JMO Messages postés 1854 Date d'inscription jeudi 23 mai 2002 Statut Membre Dernière intervention 24 juin 2018 27
17 févr. 2009 à 15:59
 Bonjour,

repertoire_du_dossier = "d:"

For Each mFichier In fso.getfolder(repertoire_du_dossier).Files
    If InStr(mFichier.Name,"OK") Then MsgBox mFichier.Name & vbCr & mFichier
Next

jean-marc
0
bigfish_le vrai Messages postés 1835 Date d'inscription vendredi 13 mai 2005 Statut Membre Dernière intervention 20 novembre 2013 15
17 févr. 2009 à 16:36
Bonjour,

une autre methode,

Sub bil()
Dim PartFileName As String, FichierTrouve() As String, NbFiles As Long, i As Long, FolderPath As String
PartFileName = "OK"
FolderPath = "c:\mon repertoire test"
With Application.FileSearch
        .NewSearch
        .LookIn = FolderPath 'Look in path
        .Filename = PartFileName 'all files that contain this string will be selected
        .SearchSubFolders = False
        .Execute
        NbFiles = .FoundFiles.Count 'how many files ?
        If NbFiles = 0 Then 'if finally no file found
            MsgBox "Cannot find any file. Please, check the path specified. ", vbCritical, "Files error..."
            Exit Sub
        End If
        ReDim ThemFiles(NbFiles) 'we dimension the table according the file(s) number
        For i = 1 To NbFiles 'for each files found
            MsgBox Dir(.FoundFiles(i))
        Next i
        .LookIn = ""
    End With
End Sub
0
Rejoignez-nous