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.
Voilà la boucle en question :
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 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
Merci d'avance pour votre aide.
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
Partager