Microsoft 365 Générer un word

Drimo_8885

XLDnaute Nouveau
Bonjour,
J'ai le fichier joint comme exemple (ma BD est plus conséquente) et j'aimerais une macro qui puisse me générer un word pour chaque étudiant comportant juste les colonnes (matière; note et coeff) avec le nombre exacte de ligne pour chacun.
La prémière colonne nom devant servir à nommer le word en question. Est-ce faisable ?
Merci pour vos retours !
Drimo.
 

Pièces jointes

  • Generer word.xlsx
    10.8 KB · Affichages: 10

sousou

XLDnaute Barbatruc
Bonjour.
Voici un début de réponse à adapter.
Ci-joint un dossier comprenant ton fichier avec la macro ('carte') cherche pas pour le nom!
et un fichier word vierge

Le tout à mettre au point, j'ai simplifié au max en fonction de ton exemple
 

Pièces jointes

  • Genererword.zip
    18.3 KB · Affichages: 6

job75

XLDnaute Barbatruc
Bonsoir Drimo_8885, sousou,

A mon avis il est mieux de générer des fichiers PDF :
VB:
Sub PDF()
Dim chemin$, d As Object, i&, a
chemin = ThisWorkbook.Path & "\" 'à adapter
Set d = CreateObject("Scripting.Dictionary")
d.CompareMode = vbTextCompare
With [C2].CurrentRegion
    '---zone d'impression et zoom---
    .Parent.PageSetup.PrintArea = .Address
    .Parent.PageSetup.Zoom = 200
    '---liste des noms sans doublon---
    For i = 2 To .Rows.Count
        d(.Cells(i, 1).Value) = ""
    Next
    If d.Count = 0 Then Exit Sub
    a = d.keys
    '---filtrage et création des fichiers PDF---
    Application.ScreenUpdating = False
    .Columns(1).Hidden = True 'masque la colonne
    For i = 0 To UBound(a)
        .AutoFilter 1, a(i)
        .ExportAsFixedFormat xlTypePDF, chemin & a(i) & ".pdf"
    Next
    .Columns(1).Hidden = False 'affiche la colonne
    If .Parent.FilterMode Then .Parent.ShowAllData 'RAZ
End With
End Sub
A+
 

Pièces jointes

  • Generer PDF(1).xlsm
    20.3 KB · Affichages: 7

Drimo_8885

XLDnaute Nouveau
Bonjour.
Voici un début de réponse à adapter.
Ci-joint un dossier comprenant ton fichier avec la macro ('carte') cherche pas pour le nom!
et un fichier word vierge

Le tout à mettre au point, j'ai simplifié au max en fonction de ton exemple
Bonsoir Sousou,

Merci énormément pour ta réponse ! ça a fonctionné sur mon exemple, je vais l'adapter à ma BD !

Bonne soirée !
 

Drimo_8885

XLDnaute Nouveau
Bonsoir Drimo_8885, sousou,

A mon avis il est mieux de générer des fichiers PDF :
VB:
Sub PDF()
Dim chemin$, d As Object, i&, a
chemin = ThisWorkbook.Path & "\" 'à adapter
Set d = CreateObject("Scripting.Dictionary")
d.CompareMode = vbTextCompare
With [C2].CurrentRegion
    '---zone d'impression et zoom---
    .Parent.PageSetup.PrintArea = .Address
    .Parent.PageSetup.Zoom = 200
    '---liste des noms sans doublon---
    For i = 2 To .Rows.Count
        d(.Cells(i, 1).Value) = ""
    Next
    If d.Count = 0 Then Exit Sub
    a = d.keys
    '---filtrage et création des fichiers PDF---
    Application.ScreenUpdating = False
    .Columns(1).Hidden = True 'masque la colonne
    For i = 0 To UBound(a)
        .AutoFilter 1, a(i)
        .ExportAsFixedFormat xlTypePDF, chemin & a(i) & ".pdf"
    Next
    .Columns(1).Hidden = False 'affiche la colonne
    If .Parent.FilterMode Then .Parent.ShowAllData 'RAZ
End With
End Sub
A+
Bonsoir Job75,

Merci énormément, c'est encore mieux en pdf oui !

Bonne soirée !
 

job75

XLDnaute Barbatruc
Oui maintenant ça va sousou.

Mais on peut aussi appliquer la même méthode de filtrage que pour les fichiers PDF :
VB:
Sub Word()
Dim chemin$, d As Object, i&, a, Wapp As Object
chemin = ThisWorkbook.Path & "\" 'à adapter
Set d = CreateObject("Scripting.Dictionary")
d.CompareMode = vbTextCompare
With [C2].CurrentRegion
    '---liste des noms sans doublon---
    For i = 2 To .Rows.Count
        d(.Cells(i, 1).Value) = ""
    Next
    If d.Count = 0 Then Exit Sub
    a = d.keys
    '---filtrage et création des fichiers Word---
    Application.ScreenUpdating = False
    .Columns(1).Hidden = True 'masque
    For i = 0 To UBound(a)
        .AutoFilter 1, a(i)
        .Copy 'copier
        If Wapp Is Nothing Then Set Wapp = CreateObject("Word.Application"): Wapp.Visible = True
        With Wapp.Documents.Add
            .Content.Paste 'coller
            .Content.ParagraphFormat.SpaceBefore = 6 'cadrage vertical
            .SaveAs chemin & a(i) & ".docx"
        End With
        Application.CutCopyMode = 0
    Next
    .Columns(1).Hidden = False 'affiche
    If .Parent.FilterMode Then .Parent.ShowAllData 'RAZ
    Wapp.Quit 'ferme Word
End With
End Sub
 

Pièces jointes

  • Generer Word(1).xlsm
    21.4 KB · Affichages: 2

Drimo_8885

XLDnaute Nouveau
Oui maintenant ça va sousou.

Mais on peut aussi appliquer la même méthode de filtrage que pour les fichiers PDF :
VB:
Sub Word()
Dim chemin$, d As Object, i&, a, Wapp As Object
chemin = ThisWorkbook.Path & "\" 'à adapter
Set d = CreateObject("Scripting.Dictionary")
d.CompareMode = vbTextCompare
With [C2].CurrentRegion
    '---liste des noms sans doublon---
    For i = 2 To .Rows.Count
        d(.Cells(i, 1).Value) = ""
    Next
    If d.Count = 0 Then Exit Sub
    a = d.keys
    '---filtrage et création des fichiers Word---
    Application.ScreenUpdating = False
    .Columns(1).Hidden = True 'masque
    For i = 0 To UBound(a)
        .AutoFilter 1, a(i)
        .Copy 'copier
        If Wapp Is Nothing Then Set Wapp = CreateObject("Word.Application"): Wapp.Visible = True
        With Wapp.Documents.Add
            .Content.Paste 'coller
            .Content.ParagraphFormat.SpaceBefore = 6 'cadrage vertical
            .SaveAs chemin & a(i) & ".docx"
        End With
        Application.CutCopyMode = 0
    Next
    .Columns(1).Hidden = False 'affiche
    If .Parent.FilterMode Then .Parent.ShowAllData 'RAZ
    Wapp.Quit 'ferme Word
End With
End Sub
Bonjour Job75,

Cette dernière version me va parfaitement.
J'aimerais juste rajouter un titre au tableau généré qui sera en haut du tableau et souligné et qui portera aussi le nom de l'élève.

Merci déjà beaucoup à vous pour ce beau travail !
 

job75

XLDnaute Barbatruc
J'aimerais juste rajouter un titre au tableau généré
Voyez ce fichier (2) et la macro complétée :
VB:
Sub Word()
Dim chemin$, d As Object, i&, a, Wapp As Object, Wdoc As Object
chemin = ThisWorkbook.Path & "\" 'à adapter
Set d = CreateObject("Scripting.Dictionary")
d.CompareMode = vbTextCompare
With [C2].CurrentRegion
    '---liste des noms sans doublon---
    For i = 2 To .Rows.Count
        d(.Cells(i, 1).Value) = ""
    Next
    If d.Count = 0 Then Exit Sub
    a = d.keys
    '---filtrage et création des fichiers Word---
    Application.ScreenUpdating = False
    .Columns(1).Hidden = True 'masque
    For i = 0 To UBound(a)
        .AutoFilter 1, a(i)
        If Wapp Is Nothing Then Set Wapp = CreateObject("Word.Application"): Wapp.Visible = True
        Set Wdoc = Wapp.Documents.Add
        Wdoc.Content = a(i) & vbLf
        Wdoc.Paragraphs(1).Range.Font.Bold = True 'gras
        Wdoc.Paragraphs(1).Range.Font.Underline = 1 'wdUnderlineSingle
        .Copy 'copier
        Wdoc.Paragraphs(2).Range.Paste 'coller
        Wdoc.Content.ParagraphFormat.SpaceBefore = 6 'cadrage vertical
        Wdoc.SaveAs chemin & a(i) & ".docx"
        Application.CutCopyMode = 0
    Next
    .Columns(1).Hidden = False 'affiche
    If .Parent.FilterMode Then .Parent.ShowAllData 'RAZ
    Wapp.Quit 'ferme Word
End With
End Sub
 

Pièces jointes

  • Generer Word(2).xlsm
    21.2 KB · Affichages: 1

job75

XLDnaute Barbatruc
Contrairement à ce que je pensais la création du titre dans Word ne vide pas le presse-papiers.

Donc on peut mettre .Copy juste après le filtrage, c'est un peu plus simple; fichier (2 bis) :
VB:
Sub Word()
Dim chemin$, d As Object, i&, a, Wapp As Object
chemin = ThisWorkbook.Path & "\" 'à adapter
Set d = CreateObject("Scripting.Dictionary")
d.CompareMode = vbTextCompare
With [C2].CurrentRegion
    '---liste des noms sans doublon---
    For i = 2 To .Rows.Count
        d(.Cells(i, 1).Value) = ""
    Next
    If d.Count = 0 Then Exit Sub
    a = d.keys
    '---filtrage et création des fichiers Word---
    Application.ScreenUpdating = False
    .Columns(1).Hidden = True 'masque
    For i = 0 To UBound(a)
        .AutoFilter 1, a(i)
        .Copy 'copier
        If Wapp Is Nothing Then Set Wapp = CreateObject("Word.Application"): Wapp.Visible = True
        With Wapp.Documents.Add
            .Content = a(i) & vbLf
            .Paragraphs(1).Range.Font.Bold = True 'gras
            .Paragraphs(1).Range.Font.Underline = 1 'wdUnderlineSingle
            .Paragraphs(2).Range.Paste 'coller
            .Content.ParagraphFormat.SpaceBefore = 6 'cadrage vertical
            .SaveAs chemin & a(i) & ".docx"
        End With
        Application.CutCopyMode = 0
    Next
    .Columns(1).Hidden = False 'affiche
    If .Parent.FilterMode Then .Parent.ShowAllData 'RAZ
    Wapp.Quit 'ferme Word
End With
End Sub
 

Pièces jointes

  • Generer Word(2 bis).xlsm
    21.3 KB · Affichages: 5

Drimo_8885

XLDnaute Nouveau
Contrairement à ce que je pensais la création du titre dans Word ne vide pas le presse-papiers.

Donc on peut mettre .Copy juste après le filtrage, c'est un peu plus simple; fichier (2 bis) :
VB:
Sub Word()
Dim chemin$, d As Object, i&, a, Wapp As Object
chemin = ThisWorkbook.Path & "\" 'à adapter
Set d = CreateObject("Scripting.Dictionary")
d.CompareMode = vbTextCompare
With [C2].CurrentRegion
    '---liste des noms sans doublon---
    For i = 2 To .Rows.Count
        d(.Cells(i, 1).Value) = ""
    Next
    If d.Count = 0 Then Exit Sub
    a = d.keys
    '---filtrage et création des fichiers Word---
    Application.ScreenUpdating = False
    .Columns(1).Hidden = True 'masque
    For i = 0 To UBound(a)
        .AutoFilter 1, a(i)
        .Copy 'copier
        If Wapp Is Nothing Then Set Wapp = CreateObject("Word.Application"): Wapp.Visible = True
        With Wapp.Documents.Add
            .Content = a(i) & vbLf
            .Paragraphs(1).Range.Font.Bold = True 'gras
            .Paragraphs(1).Range.Font.Underline = 1 'wdUnderlineSingle
            .Paragraphs(2).Range.Paste 'coller
            .Content.ParagraphFormat.SpaceBefore = 6 'cadrage vertical
            .SaveAs chemin & a(i) & ".docx"
        End With
        Application.CutCopyMode = 0
    Next
    .Columns(1).Hidden = False 'affiche
    If .Parent.FilterMode Then .Parent.ShowAllData 'RAZ
    Wapp.Quit 'ferme Word
End With
End Sub
Bonsoir Job75,

Merci beaucoup, tu gères grave !

Excellente soirée à toi,
Drimo
 

Drimo_8885

XLDnaute Nouveau
Bonsoir Job75,

Merci beaucoup, tu gères grave !

Excellente soirée à toi,
Drimo
Contrairement à ce que je pensais la création du titre dans Word ne vide pas le presse-papiers.

Donc on peut mettre .Copy juste après le filtrage, c'est un peu plus simple; fichier (2 bis) :
VB:
Sub Word()
Dim chemin$, d As Object, i&, a, Wapp As Object
chemin = ThisWorkbook.Path & "\" 'à adapter
Set d = CreateObject("Scripting.Dictionary")
d.CompareMode = vbTextCompare
With [C2].CurrentRegion
    '---liste des noms sans doublon---
    For i = 2 To .Rows.Count
        d(.Cells(i, 1).Value) = ""
    Next
    If d.Count = 0 Then Exit Sub
    a = d.keys
    '---filtrage et création des fichiers Word---
    Application.ScreenUpdating = False
    .Columns(1).Hidden = True 'masque
    For i = 0 To UBound(a)
        .AutoFilter 1, a(i)
        .Copy 'copier
        If Wapp Is Nothing Then Set Wapp = CreateObject("Word.Application"): Wapp.Visible = True
        With Wapp.Documents.Add
            .Content = a(i) & vbLf
            .Paragraphs(1).Range.Font.Bold = True 'gras
            .Paragraphs(1).Range.Font.Underline = 1 'wdUnderlineSingle
            .Paragraphs(2).Range.Paste 'coller
            .Content.ParagraphFormat.SpaceBefore = 6 'cadrage vertical
            .SaveAs chemin & a(i) & ".docx"
        End With
        Application.CutCopyMode = 0
    Next
    .Columns(1).Hidden = False 'affiche
    If .Parent.FilterMode Then .Parent.ShowAllData 'RAZ
    Wapp.Quit 'ferme Word
End With
End Sub
Rebonjour Job75,


Désolé de revenir encore, mais finalement je veux que le titre porte le contenu d'une autre colonne autre que C2, j'ai bien rajouter une ligne de code mais ça ne fonctionne pas.


Encore merci !
 

Discussions similaires

Statistiques des forums

Discussions
312 198
Messages
2 086 114
Membres
103 121
dernier inscrit
SophieS