Générer un document récapitulatif sur word à partir d'un fichier Excel

Jeepy

XLDnaute Nouveau
Bonjour ou bonsoir chers tous,

J'ai créé un fichier de suivi du paiement des loyers par les locataires, tant bien que mal...

Je souhaiterais, à partir, d'un lien ou d'un bouton, pouvoir sortir un état récapitulatif sur Word indiquant certaines informations.

Je crois que cela est possible. J'ai vu des problèmes semblables résolus, notamment par
tatiak à dit:
.

Merci de m'aider.
 

Pièces jointes

  • Exple1.xlsx
    23.9 KB · Affichages: 82
  • recaploc.docx
    12 KB · Affichages: 92

job75

XLDnaute Barbatruc
Re : Générer un document récapitulatif sur word à partir d'un fichier Excel

Bonjour Jeepy, bienvenue sur XLD, bonjour le forum,

Plutôt qu'un document WORD pourquoi ne pas créer un fichier PDF ?

Vous trouverez les 2 solutions dans les fichiers joints avec ces macros dans les feuilles "BDD" :

Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Target.Row < 8 Or Target.Column <> 8 Or Target = "" Then Exit Sub
Dim Wapp As Object, Wdoc As Object, nomfich$
Cancel = True
Target.EntireRow.Name = "P" 'plage nommée
On Error Resume Next
Set Wapp = GetObject(, "Word.Application") 'si Word est ouvert
If Wapp Is Nothing Then Set Wapp = CreateObject("Word.Application")
Wapp.Visible = True
Set Wdoc = Wapp.Documents.Add 'nouveau document
Feuil2.[A1:F25].Copy: Wapp.Selection.Paste: Application.CutCopyMode = 0 'copier-coller
With Wdoc.Content.ParagraphFormat 'mise en forme
  .SpaceBefore = 3
  .SpaceAfter = 3
End With
nomfich = Target & Format(Date, " yyyy-mm-dd")
Wapp.Documents(nomfich).Close False 'si le fichier Word est ouvert il est fermé
On Error GoTo 0
Wdoc.SaveAs ThisWorkbook.Path & "\" & nomfich
Set Wapp = Nothing
MsgBox "Le document WORD '" & nomfich & "' a été créé..."
End Sub
Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Target.Row < 8 Or Target.Column <> 8 Or Target = "" Then Exit Sub
Dim nomfich$
Cancel = True
Target.EntireRow.Name = "P" 'plage nommée
nomfich = Target & Format(Date, " yyyy-mm-dd")
Feuil2.ExportAsFixedFormat xlTypePDF, ThisWorkbook.Path & "\" & nomfich
MsgBox "Le document PDF '" & nomfich & "' a été créé..."
End Sub
Double-clic sur un nom en colonne H pour lancer la macro.

Tout cela est assez simple, la vraie difficulté consiste à réaliser une bas de données qui tienne la route (la vôtre est à améliorer et compléter) et à créer une feuille "Récap" satisfaisante, avec les bonnes formules.

A+
 

Pièces jointes

  • Excel-Word(1).xlsm
    37.4 KB · Affichages: 86
  • Excel-PDF(1).xlsm
    38.1 KB · Affichages: 71

job75

XLDnaute Barbatruc
Re : Générer un document récapitulatif sur word à partir d'un fichier Excel

Bonjour Jeepy,

Si l'on veut que le document Word soit fermé :

Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Target.Row < 8 Or Target.Column <> 8 Or Target = "" Then Exit Sub
Dim Wapp As Object, ferme As Boolean, Wdoc As Object, nomfich$
Cancel = True
Target.EntireRow.Name = "P" 'plage nommée
On Error Resume Next
Set Wapp = GetObject(, "Word.Application") 'si Word est ouvert
If Wapp Is Nothing Then Set Wapp = CreateObject("Word.Application"): ferme = True
Wapp.Visible = True
Set Wdoc = Wapp.Documents.Add 'nouveau document
Feuil2.[A1:F25].Copy: Wapp.Selection.Paste: Application.CutCopyMode = 0 'copier-coller
With Wdoc.Content.ParagraphFormat 'mise en forme
  .SpaceBefore = 3
  .SpaceAfter = 3
End With
nomfich = Target & Format(Date, " yyyy-mm-dd")
Wapp.Documents(nomfich).Close False 'si le fichier Word est ouvert il est fermé
On Error GoTo 0
Wdoc.SaveAs ThisWorkbook.Path & "\" & nomfich
If ferme Then Wapp.Quit 'ferme Word
Set Wapp = Nothing
MsgBox "Le document WORD '" & nomfich & "' a été créé..."
End Sub
Fichier (2).

Bonne journée.
 

Pièces jointes

  • Excel-Word(2).xlsm
    37.6 KB · Affichages: 108

Discussions similaires