IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Macros et VBA Excel Discussion :

Problème boucle For, VBA


Sujet :

Macros et VBA Excel

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Homme Profil pro
    Étudiant
    Inscrit en
    Février 2020
    Messages
    17
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Étudiant
    Secteur : Industrie

    Informations forums :
    Inscription : Février 2020
    Messages : 17
    Par défaut Problème boucle For, VBA
    Bonjour,

    J'ai conçu une macro sur excel, dont l'objectif est de récupérer tous les graphiques présents dans un classeur excel et commençant par le même nom (exemple : Graphique).

    Et d'ensuite les copier sous forme d'image dans un fichier Word.

    Ça fonctionne, sauf qu'au lieu d'afficher tous les graphiques, il semblerait que la macro écrase les précédents pour afficher que le dernier.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
     
    Sub Export_Chart_Word()
     
     
        'Name of an existing Word document, and the name the chart will have when exported.
     
        Const stWordDocument As String = "C:\Users\xxxxxx\Documents\ChartReport.docx"
        Const stChartName As String = "ChartReport.png"
     
        'Word objects.
        Dim wdApp As Word.Application
        Dim wdDoc As Word.Document
        Dim wdbmRange As Word.Range
     
        'Excel objects.
        Dim wbBook As Workbook
        Dim wsSheet As Worksheet
        Dim ChartObj As ChartObject
     
        'Initialize the Excel objects.
        Set wbBook = ThisWorkbook
        Set wsSheet = ActiveSheet
     
     
     
        For Each Graphique In wsSheet.ChartObjects
         If Left(Graphique.Name, 5) = "Chart" Then Set ChartObj = Graphique
        ChartObj.Chart.Export _
                       Filename:=wbBook.Path & "\" & Graphique.Name, _
                       FilterName:="PNG"
     
        Next
     
     
     
        'Turn off screen updating.
        'Application.ScreenUpdating = False
     
     
        'Initialize the Word objects to the existing Word document and bookmark.
        Set wdApp = New Word.Application
        Set wdDoc = wdApp.Documents.Open(stWordDocument)
        Set wdbmRange = wdDoc.Bookmarks("ChartReport").Range
     
     
     
        'If there is already an inline shape, that means the macro has been run before - clean up any artifacts.
        On Error Resume Next
        With wdDoc.InlineShapes(1)
            .Select
            .Delete
        End With
        On Error GoTo 0
     
        'Add the .gif file to the document at the bookmarked location,
        'and ensure that it is saved inside the Word doc.
        With wdbmRange
            .Select
            .InlineShapes.AddPicture _
            Filename:=wbBook.Path & "\" & stChartName, _
            LinkToFile:=False, _
            savewithdocument:=True
        End With
     
        'Save and close the Word document.
        With wdDoc
            .Save
            .Close
        End With
     
        'Quit Word.
        wdApp.Quit
     
        'Clear the variables.
        Set wdbmRange = Nothing
        Set wdDoc = Nothing
        Set wdApp = Nothing
     
        'Delete the temporary . file.
        On Error Resume Next
        Kill wbBook.Path & "C:\Users\xxxxxx\Documents" & stChartName
        On Error GoTo 0
     
        MsgBox "Chart exported successfully to " & stWordDocument
     
    End Sub
    Voilà la boucle en question :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
     
     For Each Graphique In wsSheet.ChartObjects
         If Left(Graphique.Name, 5) = "Chart" Then Set ChartObj = Graphique
        ChartObj.Chart.Export _
                       Filename:=wbBook.Path & "\" & Graphique.Name, _
                       FilterName:="PNG"
     
        Next
    Merci d'avance pour votre aide.

  2. #2
    Membre averti
    Homme Profil pro
    Étudiant
    Inscrit en
    Février 2020
    Messages
    17
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Étudiant
    Secteur : Industrie

    Informations forums :
    Inscription : Février 2020
    Messages : 17
    Par défaut
    Up ?

Discussions similaires

  1. [XL-2016] Problème boucle For VBA
    Par Entei dans le forum Macros et VBA Excel
    Réponses: 0
    Dernier message: 11/02/2020, 10h37
  2. Problème boucle for
    Par rouliane dans le forum C++
    Réponses: 2
    Dernier message: 14/12/2006, 14h37
  3. [VBA-E, Débutant] Problème Boucle for
    Par strifer dans le forum Macros et VBA Excel
    Réponses: 10
    Dernier message: 24/08/2006, 12h30
  4. [Débutant] Problème boucle for
    Par toniooooo dans le forum Langage
    Réponses: 10
    Dernier message: 18/04/2006, 14h42
  5. [FLASH 8] Problème Boucle For
    Par Begood dans le forum Flash
    Réponses: 6
    Dernier message: 21/03/2006, 11h36

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo