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
| Sub Reçus_Mail_Séparés()
'Nécessite d'activer la référence "Microsoft Word xx.x Object Library"
Dim docWord As Word.Document
Dim appWord As Word.Application
Dim NomBase As String
Dim NomFicherWord As String
NomBase = ThisWorkbook.Path & "\Base reçus par Mail.xls"
NomFicherWord = ThisWorkbook.Path & "\Reçu Lettre Type.doc"
Application.DisplayAlerts = False
Application.ScreenUpdating = False
Set appWord = New Word.Application
appWord.Visible = True
'Ouverture du document principal Word :
Set docWord = appWord.Documents.Open(NomFicherWord)
'Fonctionnalité de publipostage pour le document spécifié :
With docWord.MailMerge
.MainDocumentType = wdFormLetters
'Ouvre la base de données :
.OpenDataSource Name:=NomBase, _
Connection:="Driver={Microsoft Excel Driver (*.xls)};" & _
"DBQ=" & NomBase & "; ReadOnly=True;", _
SQLStatement:="SELECT * FROM [Abonnés$]", _
SQLStatement1:="", SubType:=wdMergeSubTypeAccess
' Déclaration des variables
Dim iR As Long
Dim I As Long
Dim DocName As String
Application.ScreenUpdating = False
iR = docWord.MailMerge.DataSource.RecordCount
Debug.Print iR
For I = 1 To iR
With docWord.MailMerge
'Définition du premier et dernier enregistrement
.DataSource.FirstRecord = I
.DataSource.LastRecord = I
' Envoi des données dans un nouveau document
.Destination = wdSendToNewDocument
' Exécution du publipostage
Application.ScreenUpdating = False
.Execute
'Affichage du message d'attente :
Load Patience2
Patience2.Show vbModeless
Patience2.Repaint
' Actualisation de l'enregistrement pour la sauvegarde
.DataSource.ActiveRecord = I
'Utilisation de deux champs pour obtenir le nom du document
DocName = .DataSource.DataFields(3).Value
DocName = DocName & "-" & .DataSource.DataFields(2).Value
'DocName = DocName & "-" & "Don n" & .DataSource.DataFields(9).Value
Debug.Print DocName; I
End With
' Sauvegarde du document publiposté
With appWord.ActiveDocument
.SaveAs ThisWorkbook.Path & "\Dons Reçus par Mail " & An & "\" & DocName & " Don " & An & ".doc"
.Close
End With
Next I
End With
Application.ScreenUpdating = True
Application.DisplayAlerts = True
'Fermeture du document Word
docWord.Close False
appWord.Quit
Call Reçus_Mail_Collés
End Sub |
Partager