Sub WORD_MAJ()
'Copier les données et les graphiques Excel dans le document Word
Application.ScreenUpdating = False
Application.DisplayAlerts = False
'On Error GoTo message:
'01) Déclaration de variable(s)
Dim strClasseurExcelTraitement As String
'Word
Dim strSignet As String 'Signet dans le document Word
'Excel
Dim bytGraphiques As Byte 'Nombre de graphiques
Dim strFeuilleGraphique As String 'Feuille Excel contenant les graphiques
Dim strGaphique As String 'Chaque graphique Excel
strClasseurExcelTraitement = ActiveWorkbook.Name
'02) Initialisation de variable(s)
'Word
Set objApplication = New Word.Application
strDossier_Fichiers = Range("Dossier_Fichiers").Value
strChemin_Photos = Range("Chemin_Photos").Value
strNom_Photo1 = Range("Nom_Photo1").Value
strNom_Photo2 = Range("Nom_Photo2").Value
'Excel
bytChamps = 15
bytGraphiques = Range("Nombre_Graphiques").Value
'03) Utilisation de variable(s)
'Word
With objApplication
'03-1) Ouvrir Word
.Visible = True
'03-2) Sélectionner le dossier Word
.ChangeFileOpenDirectory strDossier_Fichiers
'03-3) Ouvrir le document Word
.Documents.Open Filename:=strDocumentWord
End With
'03-4) Boucle des données
For bytI = 1 To bytChamps
'INITIALISATION
Windows(strClasseurExcelTraitement).Activate
Sheets("Données").Select 'Sélectionner la feuille "Données"
Application.CutCopyMode = False
'Word
strSignet = Application.Index(Range("table_donnees"), 3, bytI)
'****************************************************************
'UTILISATION
'Excel
Range(Cells(5, bytI), Cells(5, bytI)).Select
Selection.Copy
'Word
With objApplication
'1) Activer Word
.Activate
'2) Atteindre le signet
.Selection.Goto what:=wdGoToBookmark, Name:=strSignet
'3) Effacer la donnée à droite du signet
.Selection.MoveRight Unit:=wdCell
.Selection.Delete Unit:=wdCharacter, Count:=1
'4) Inscrire la donnée en "Collage spécial Texte mis en forme"
.Selection.PasteExcelTable False, False, False
' .Selection.Font.Color = RGB(51, 104, 90)
.Selection.TypeBackspace 'retour arrière car crée une seconde ligne après avoir copié
Application.CutCopyMode = False
End With
Application.CutCopyMode = False
Next bytI
'03-5) Boucle des graphiques
For bytI = 1 To bytGraphiques
'INITIALISATION
Sheets("Graphiques").Select 'Sélectionner la feuille "Graphiques"
'Excel
strFeuilleGraphique = Application.Index(Range("table_graphiques"), bytI, 1)
strGaphique = Application.Index(Range("table_graphiques"), bytI, 2)
'Word
strSignet = Application.Index(Range("table_graphiques"), bytI, 3)
'****************************************************************
'UTILISATION
'Excel
Sheets(strFeuilleGraphique).Select 'Sélectionner la feuille du graphique
ActiveSheet.ChartObjects(strGaphique).Select 'Sélectionner le graphique
ActiveSheet.ChartObjects(strGaphique).Copy 'Copier le graphique
'Word
With objApplication
'1) Activer Word
.Activate
'2) Sélectionner le signet
.Selection.Goto what:=wdGoToBookmark, Name:=strSignet
'3) Effacer le graphique à droite du signet
.Selection.MoveRight Unit:=wdCell
.Selection.Delete Unit:=wdCharacter, Count:=1
'4) Inscrire le graphique
.Selection.Paste
Application.CutCopyMode = False
End With
Application.CutCopyMode = False
Next bytI
'*********************************************************************
'03-6 Insère les photos
objApplication.Run "INSERTION_PHOTOS_ACTUALISATION", strChemin_Photos, strNom_Photo1, strNom_Photo2
'03-7) Enregistrer le document Word
'Aller au début du document
objApplication.Selection.HomeKey Unit:=wdStory
objApplication.ActiveDocument.Save
'04) Fin du traitement
'Libérer la mémoire vive avec la variable objet
Set objApplication = Nothing
Exit Sub
Application.ScreenUpdating = True
Application.DisplayAlerts = True
message: MsgBox "Erreur Word Mise à jour"
End Sub