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
| Option Explicit
Sub Publipostage()
Dim Chemin, NbreX, FileMailing
Dim DocWord As Word.Document
Dim NomBase
Application.ScreenUpdating = False
Chemin = ThisWorkbook.Path
With Sheets("feuil1")
.Activate
NbreX = Application.CountIf(.Range(.[H2], .[H65536]), "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
'Sheets(Array("feuil1", "Listes")).Copy
Sheets("feuil1").Copy
Application.DisplayAlerts = False
ActiveWorkbook.SaveAs Filename:=Chemin & "\Temp.xlsx", FileFormat:=51
ActiveWorkbook.Close savechanges:=True
Application.DisplayAlerts = True
ChDir ThisWorkbook.Path
FileMailing = Application.GetOpenFilename("Fichiers Word (*.doc), *.doc", , "Ouvrir le document Word pour le mailing d'étiquettes ...")
If FileMailing = False Then End
'Si c'est OK on incrémente la référence
'Sheets("Feuil2").[A2] = [A2] + 1
' et efface la croix de la colonne H
With Sheets("Feuil1")
'.Range(.Cells(2, 8), .Cells(.Rows.Count.End(xlUp).Row, 8)).ClearContents
.Range(.[H2], .[H65536]).ClearContents
End With
' Ouverture de Word
Dim AppWord As Word.Application
Set AppWord = New Word.Application
AppWord.Visible = True 'False 'True
Set DocWord = AppWord.Documents.Open(FileMailing)
NomBase = Chemin & "\Temp.xlsx"
With DocWord.MailMerge
.OpenDataSource Name:=NomBase, _
Connection:="Driver={Microsoft Excel Driver (*.xls)};" & "DBQ=" & _
NomBase & "; ReadOnly=True;", SQLStatement:="SELECT * FROM [feuil1$] WHERE [ETIQUETTE] like 'x' OR [ETIQUETTE] like 'X'"
'Spécifie la fusion vers un nouveau document (wdSendToPrinter= Vers l'imprimante)
.Destination = wdSendToNewDocument
'.SuppressBlankLines = True 'Il ne peut pas y voir de ligne blanche car on demande celle qui ont des croix
'Prend en compte l'ensemble des enregistrements
With .DataSource
.FirstRecord = wdDefaultFirstRecord
.LastRecord = wdDefaultLastRecord
End With
'Exécute l'opération de publipostage
.Execute Pause:=False
End With
' Activation du doucment principal de Publipostage et fermeture
DocWord.Activate
DocWord.Close savechanges:=False
' Affichage l'application Word
AppWord.Visible = True
Set DocWord = Nothing
Set AppWord = Nothing
' Activation de l'onglet
' Effacement du fichier temporaire crée spécialement pour la fusion
Kill Chemin & "\temp.xlsx"
Application.ScreenUpdating = True
End Sub |
Partager