XL 2019 Copier coller de Excel vers word

ben haj salah

XLDnaute Nouveau
Bonjour tout le monde

Voilà, j'ai un fichier excel contenantdes tableau, et je dois réaliser un script en vba permettant de créer un document word et d'y insérer le tableau après le titre que je définis.

Quelqu'un aurait t'il une petite aide sur mon problème du collage des données après un titre.

Merci
 

Pièces jointes

  • CE.docx
    11.6 KB · Affichages: 9
  • ESSAI.xlsx
    9.9 KB · Affichages: 3

job75

XLDnaute Barbatruc
Bonjour ben haj salah, bienvenue sur XLD,

Téléchargez les fichiers joints dans le même dossier (le bureau).

Puis lancez cette macro en cliquant sur le bouton :
VB:
Sub Copier_vers_Word()
Dim Wapp As Object, Wdoc As Object
On Error Resume Next
Set Wapp = GetObject(, "Word.Application")
If Wapp Is Nothing Then Set Wapp = CreateObject("Word.Application")
Wapp.Visible = True
Set Wdoc = Wapp.documents.Open(ThisWorkbook.Path & "\CE.docx") 'chemin et nom à adapter
If Wdoc Is Nothing Then MsgBox "Document Word introuvable !", 48: Exit Sub
Wapp.Selection.WholeStory
Wapp.Selection.Delete 'RAZ
Range("A1", [Tableau1]).Copy 'Copier
Wapp.Selection.Paste 'Coller
Application.CutCopyMode = 0
AppActivate "Word" 'activation facultative
End Sub
Pour voir le code allez dans VBA par Alt+F11.

A+
 

Pièces jointes

  • ESSAI(1).xlsm
    18.7 KB · Affichages: 7
  • CE.docx
    11.6 KB · Affichages: 8

job75

XLDnaute Barbatruc
Fichier (2) avec Mise en page (centrage) des paragraphes :
VB:
Sub Copier_vers_Word()
Dim Wapp As Object, Wdoc As Object
On Error Resume Next
Set Wapp = GetObject(, "Word.Application")
If Wapp Is Nothing Then Set Wapp = CreateObject("Word.Application")
Wapp.Visible = True
Set Wdoc = Wapp.documents.Open(ThisWorkbook.Path & "\CE.docx") 'chemin et nom à adapter
If Wdoc Is Nothing Then MsgBox "Document Word introuvable !", 48: Exit Sub
Wapp.Selection.WholeStory
Range("A1", [Tableau1]).Copy 'Copier
Wapp.Selection.Paste 'Coller
Application.CutCopyMode = 0
'---Mise en page---
Wapp.Selection.WholeStory
With Wapp.Selection.ParagraphFormat
    .SpaceBefore = 3
    .SpaceAfter = 3
End With
Wapp.Selection.EndKey 6 'wdStory
AppActivate "Word" 'activation facultative
End Sub
 

Pièces jointes

  • ESSAI(2).xlsm
    19 KB · Affichages: 5

Discussions similaires

Statistiques des forums

Discussions
312 097
Messages
2 085 261
Membres
102 844
dernier inscrit
atori2