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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
| Option Compare Database
Option Explicit
Sub PayeGM1_Indiv()
Dim wApp As Word.Application
Dim wDoc As Word.Document
Dim chemin As String
Dim sqlA As String, sqlS As String, sqlB As String, sqlC As String
Dim rsA As DAO.Recordset, rsS As DAO.Recordset, rsB As DAO.Recordset, rsC As DAO.Recordset
Dim db As DAO.Database
Dim dbNumeroRupt As Double ' pour mémoriser le N° adhérent en cours
Dim cptLigneAdr As Long
Set db = CurrentDb
'sqlA = "SELECT * FROM R_Publipostage_adherents ORDER BY nom_adhe;"
sqlA = "SELECT * FROM R_Publipostage_adherents" & _
" LEFT JOIN R_Publipostage_Indemnisations" & _
" ON R_Publipostage_adherents.numero = R_Publipostage_Indemnisations.num_adhe" & _
" WHERE Nz([R_Publipostage_Indemnisations.num_adhe],0)='618.2';"
'Pour éditer les courriers des adhérents ayant droit à une indemnité
Set rsA = db.OpenRecordset(sqlA)
Set wApp = New Word.Application
' wApp.Visible = True
chemin = CurrentProject.Path
While Not rsA.EOF
cptLigneAdr = 0
Set wDoc = wApp.Documents.Open(chemin & "\Modèle-Bourse-Circuits.docx") 'Mettre l'année à jour
cptLigneAdr = cptLigneAdr + 1
wDoc.Bookmarks("LigneAdr" & Format(cptLigneAdr, "00")).Range.Text = rsA.Fields("civilite") & " " & UCase(rsA.Fields("nom_adhe")) & " " & rsA.Fields("prenom")
cptLigneAdr = cptLigneAdr + 1
wDoc.Bookmarks("LigneAdr" & Format(cptLigneAdr, "00")).Range.Text = rsA.Fields("adresse")
If rsA.Fields("addresse2") <> " " Then
cptLigneAdr = cptLigneAdr + 1
wDoc.Bookmarks("LigneAdr" & Format(cptLigneAdr, "00")).Range.Text = rsA.Fields("addresse2")
End If
cptLigneAdr = cptLigneAdr + 1
wDoc.Bookmarks("LigneAdr" & Format(cptLigneAdr, "00")).Range.Text = rsA.Fields("CodePostal") & " " & UCase(rsA.Fields("Ville"))
sqlB = "SELECT * FROM R_Publipostage_NombrePR WHERE numero=" & rsA.Fields("R_Publipostage_Adherents.numero")
Set rsB = db.OpenRecordset(sqlB)
If Not rsB.EOF Then wDoc.Bookmarks("Total").Range.Text = rsB.Fields("Nbr_PR")
If Not rsB.EOF Then wDoc.Bookmarks("Total1").Range.Text = rsB.Fields("Nbr_Pr")
If Not rsB.EOF Then wDoc.Bookmarks("NumAdherent").Range.Text = rsB.Fields("numero")
'--- tableau
sqlS = "SELECT * FROM R_Publipostage_Circuits WHERE numero=" & rsA.Fields("R_Publipostage_Adherents.numero")
Set rsS = db.OpenRecordset(sqlS)
While Not rsS.EOF
wDoc.Tables(1).Rows.Add
wDoc.Tables(1).Rows.Last.Cells(1).Range.Text = rsS.Fields("secteur_balirando")
wDoc.Tables(1).Rows.Last.Cells(2).Range.Text = UCase(rsS.Fields("code"))
wDoc.Tables(1).Rows.Last.Cells(3).Range.Text = rsS.Fields("nom_pr")
wDoc.Tables(1).Rows.Last.Cells(4).Range.Text = UCase(rsS.Fields("depart"))
wDoc.Tables(1).Rows.Last.Cells(5).Range.Text = rsS.Fields("AR_circuit") & " Km"
wDoc.Tables(1).Rows.Last.Cells(6).Range.Text = rsS.Fields("balisage")
wDoc.Tables(1).Rows.Last.Cells(7).Range.Text = rsS.Fields("annee_attribution")
wDoc.Tables(1).Rows.Last.Cells(7).Range.Font.Color = RGB(255, 0, 0)
' Pour faire apparaitre en rouge les circuits remis à dispositions.
If rsS.Fields("annee_attribution") = 2021 Then '(Pour bourse aux circuits 2025
wDoc.Tables(1).Rows.Last.Cells(7).Range.Font.Color = RGB(255, 0, 0)
wDoc.Tables(1).Rows.Last.Cells(7).Range.Font.Bold = True
Else
wDoc.Tables(1).Rows.Last.Cells(7).Range.Font.Color = RGB(0, 0, 0)
wDoc.Tables(1).Rows.Last.Cells(7).Range.Font.Bold = False
End If
If dbNumeroRupt <> rsA.Fields("numero") Then
dbNumeroRupt = rsA.Fields("numero")
sqlC = "SELECT * FROM R_Publipostage_Indemnisations WHERE num_adhe=" & rsA.Fields("numero")
Set rsC = db.OpenRecordset(sqlC)
wDoc.Bookmarks("TotaldistAR").Range.Text = UCase(rsC.Fields("Km_indemnisés")) & " Km"
'wDoc.Bookmarks("Retenus").Range.Text = rsC.Fields("Retenus") & " Km"
wDoc.Bookmarks("Montant").Range.Text = UCase(rsC.Fields("Montant")) & " "
wDoc.Bookmarks("Cheque").Range.Text = rsC.Fields("A percevoir") & ".00 "
'wDoc.Bookmarks("Info").Range.Text = IIf(Nz(rsC.Fields("Remarque"), "") = "0", "", Nz(rsC.Fields("Remarque"), "")) 'Permet d'afficher une information à l'adhérent
End If
rsS.MoveNext
Wend
dbNumeroRupt = 0
'sauvegarde du fichier
wDoc.SaveAs CurrentProject.Path & "\Publipostage\Indemnisations2026 Bourse circuits2027\Avec indemnisation\Adhérent unique\Circuits" & " " & rsA.Fields("nom_adhe") & " " & rsA.Fields("prenom") & ".docx"
wDoc.Close (wdDoNotSaveChanges)
rsA.MoveNext
Wend
' --- Fermeture et libération des recordsets et de la DB ---
If Not rsS Is Nothing Then
If rsS.State = dbStateOpen Then rsS.Close
Set rsS = Nothing
End If
If Not rsB Is Nothing Then
If rsB.State = dbStateOpen Then rsB.Close
Set rsB = Nothing
End If
If Not rsC Is Nothing Then
If rsC.State = dbStateOpen Then rsC.Close
Set rsC = Nothing
End If
If Not rsA Is Nothing Then
If rsA.State = dbStateOpen Then rsA.Close
Set rsA = Nothing
End If
If Not db Is Nothing Then
db.Close
Set db = Nothing
End If
' --- Fermeture de Word ---
If Not wApp Is Nothing Then
wApp.Quit
Set wApp = Nothing
End If
Dim wDoc1 As Object
Dim stFicDocs As String
Dim stRepDocs As String
Set wApp = CreateObject("Word.Application")
stRepDocs = (CurrentProject.Path)
' Fermer et libérer les objets
Set wdapp = Nothing
End Sub |
Partager