Boucle for each

Bonjour,

J'ai un petit soucis, j'ai ce bout de code :

For Each image In dossier.Files

                 If Right(image.Name, 4) = ".jpg" Then

                        nomfichier = dossier & "\" & image.Name '<-- cette ligne pose problème !
                        MsgBox nomfichier 'pour vérifier si il parcourt bien tous les jpg

                            For Each affichage In Graphiques.Controls 'Graphique est le nom de mon UF

                                affichage.Picture = LoadPicture(nomfichier)

                            Next affichage

                    End If

            Next image

mais il me fait pas tout à fait ce que je voudrais.

Arrivé à "nomfichier" il me parcourt d'un coup tout les fichiers jpg au lieu de me les faire un par un ...

Une Idée ?

Merci à vous

Bonjour AZQW,

Si tu utilises FSO, comme je suppose, il nous manque du code plus haut

Pour moi dossier à déjà le "\" au bout, donc inutile de le rajouter, à vérifier avec

Debug.print dossier

A+

Bonjour,

Tu peux d'ailleurs faire plus simple :

nomfichier = image.Path

Bonjour,

BrunoM45 : Du coup j'ai regardé, et l'antislash n'était pas présent plus haut.

thev : J'ai fait ce que tu m'as conseillé, et ça fonctionne très bien merci.

Du coup, le problème est toujours présent.

Je mets ci-dessous la macro :

Private Sub Graph_Click()

Dim tableau As PivotTable, ws As Worksheet, indicateur As Object, chemin As String, affichage As Control
Dim image As File, dossier As Folder, fso As FileSystemObject, nomfichier As String

Set ws = ActiveWorkbook.Sheets("tabdym")
Set fso = New FileSystemObject
Set dossier = fso.GetFolder("Z:\Projet\dossiermacro")

    For Each tableau In ws.PivotTables

            tableau.RefreshTable

    Next tableau

    For Each indicateur In ws.ChartObjects

            chemin = ActiveWorkbook.Path & "\" & indicateur.Name & ".jpg"
            indicateur.Chart.Export chemin

    Next indicateur

            For Each image In dossier.Files

                 If Right(image.Name, 4) = ".jpg" Then

                        nomfichier = image.Path
                        MsgBox nomfichier

                            For Each affichage In Graphiques.Controls

                                affichage.Picture = LoadPicture(nomfichier)

                            Next affichage

                    End If

            Next image

    Debug.Print dossier

    Graphiques.Show

End Sub

En mettant en pas à pas je viens de me rendre compte que le compte fait ce que je lui ai demandé (évidement comme toujours ).

C'est la 2e boucle for each qui ...... fait ça boucle et écrase tout à chaque fois

Bon faut que je réfléchisse à ça

Bon j'ai réussi non sans mal !

Voici le code pour les curieux

Private Sub Graphbutton_Click()
  Dim tableau As PivotTable, ws As Worksheet, indicateur As Object, chemin As String, affichage As Object, qte As Byte
  Dim image As File, dossier As Folder, fso As FileSystemObject, nomfichier As String

  Set ws = ActiveWorkbook.Sheets("tabdym")
  Set fso = New FileSystemObject
  Set dossier = fso.GetFolder("Z:\projet\dossiermacro")

  For Each tableau In ws.PivotTables
    tableau.RefreshTable
  Next tableau

  For Each indicateur In ws.ChartObjects
    chemin = ActiveWorkbook.Path & "\" & indicateur.Name & ".jpg"
    indicateur.Chart.Export chemin
  Next indicateur

  For Each image In dossier.Files
    If Right(image.Name, 4) = ".jpg" Then
      nomfichier = image.Path
      qte = qte + 1
      If qte = 1 Then
        Set affichage = Graphiques.graph1
      ElseIf qte = 2 Then
        Set affichage = Graphiques.graph2
      ElseIf qte = 3 Then
        Set affichage = Graphiques.graph3
      Else
        Set affichage = Graphiques.graph4
      End If
      affichage.Picture = LoadPicture(nomfichier)
    End If
  Next image
  Graphiques.Show
End Sub
Rechercher des sujets similaires à "boucle each"