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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
| Sub PublipostageTest()
Dim C As Range, Plage As Range, Sh As Worksheet, Acc As Range
Dim NbreX As Long, L As Long, Cel As Range
Dim Wd As Word.Application, WdDoc As Word.Document
Dim Chemin As String, Fichier As String, Source As String
'En supposant que le document Word est dans le
'même répertoire que le fichier Excel ouvert
Chemin = ThisWorkbook.Path & "\"
'Chemin & Nom du fichier Excel où est le tableau des données
'Ce fichier est présumé ouvert
Source = ThisWorkbook.FullName
'MsgBox "Chemin : " & Chemin & vbCrLf & _
"Fichier : " & Fichier & vbCrLf & _
"Chemin & Fichier : " & Chemin & Fichier & vbCrLf & _
"Source : " & Source
With Sheets("feuil1")
.Activate
NbreX = Application.CountIf(.Range(.[O2], .[O65536]), "x")
If NbreX = 0 Then
MsgBox "Il n'y a pas d'étiquette à extraire.", vbInformation + vbOKOnly
.Range("A1").Select
Exit Sub
End If
End With
'Attribution d'un nom "Insertion / names" de la plage de données du tableau
With Workbooks(ThisWorkbook.Name).Worksheets("Feuil1")
.Range("A1:O" & .Range("A65536").End(xlUp).Row).Name = "Données"
End With
'---------
With Sheets("Feuil1")
.AutoFilterMode = False
Set Plage = .Range(.[C1], .Cells(.Rows.Count, 3).End(xlUp))
'Plage.AdvancedFilter xlFilterInPlace, unique:=True
.Range(.[A1], .Cells(.Rows.Count, 1).End(xlUp)).Resize(, 15).AutoFilter 15, "x"
Set Plage = Plage.Offset(1).Resize(Plage.Rows.Count - 1)
Set Plage = Plage.SpecialCells(xlCellTypeVisible)
.ShowAllData
For Each C In Plage
.AutoFilterMode = False
.Range(.[A1], .Cells(.Rows.Count, 1).End(xlUp)).Resize(, 15).AutoFilter 3, C.Value
.Range(.[A1], .Cells(.Rows.Count, 1).End(xlUp)).Resize(, 15).AutoFilter 15, "x"
'on test le nombre d'occurence de C dans la plage filtrée
If Application.Subtotal(103, .[A:A]) > 2 Then
'+1 on utilise le deuxième modèle pour le publipostage avec un tableau
'MsgBox "Lignes supérieur à 2 :" & Application.Subtotal(103, .[A:A])
Fichier = "DéchargeTableau.doc"
L = 1
Else
'MsgBox "Lignes inférieur ou égal à 2 :" & Application.Subtotal(103, .[A:A])
Fichier = "Décharge.doc"
L = 0
End If
With .AutoFilter.Range
Set Acc = .Offset(1).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible)
'Création d'une instance de Word
Set Wd = CreateObject("Word.Application")
'Rendre visible ou nom l'application Word
Wd.Visible = True ' or False
'Ouverture du document pour la publication
Set WdDoc = Wd.Documents.Open(Chemin & Fichier)
With WdDoc.MailMerge
.OpenDataSource Name:=Source, _
Connection:="Driver={Microsoft Excel Driver (*.xls)};" & "DBQ=" & _
Chemin & "; ReadOnly=True;", SQLStatement:="SELECT * FROM [feuil1$] WHERE [impression] like 'x' OR [impression] like 'X'"
' Lancer l'impression du publipostage
.Destination = wdSendToNewDocument ' OU wdSendToPrinter
'parcourir la plage Acc ligne par ligne pour la fusion
For Each Cel In Acc.Column(3).Address
With .DataSource
.FirstRecord = wdDefaultFirstRecord
.LastRecord = wdDefaultLastRecord
End With
.Execute Pause:=False
'Prochaine ligne dans Acc
Next Cel
'Attente que l'impression soit terminée avant de fermé
'le document et Word
While Wd.BackgroundPrintingStatus <> 0
DoEvents
Wend
End With
WdDoc.Close SaveChanges:=wdDoNotSaveChanges 'Ferme le document
'Ferme Word
Wd.Quit wdDoNotSaveChanges
'-------------
End With
Next C
.AutoFilterMode = False
'Libère la mémoire occupée par les objects
Set WdDoc = Nothing
Set Wd = Nothing
End With
End Sub |
Partager