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 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
|
Option Explicit
Sub Prod_Facture()
'Déclaration des variables
Dim xlApp As Excel.Application
Dim xlWb As Excel.Workbook
Dim xlSh1, xlSh2, xlSh5 As Excel.Worksheet
Dim iR, jR, lR, mR As Integer
Dim i, j, k, l, m As Integer
Dim oDoc As Document
Dim oTbl, oTb2, oTb3 As Table
Dim stDocName As String
Dim stPath As String
Dim plage As Range, resultat As Integer
Dim User As String
User = "t.molierac"
'Affectation des données aux variables
Set xlApp = CreateObject("Excel.Application")
Set xlWb = xlApp.Workbooks.Open("C:\Users\" & User & "\OneDrive - LEXIQUE\RESEAU LEXIQUE\FACTURE\PROD\SGBD_Facture_Production.xlsx")
Set xlSh1 = xlWb.Worksheets(1)
Set xlSh2 = xlWb.Worksheets(2)
Set xlSh5 = xlWb.Worksheets(5)
'Récupération du nombre de lignes et de colonnes
iR = xlSh1.UsedRange.Rows.Count
jR = xlSh2.UsedRange.Rows.Count
For i = 49 To 49
stDocName = "C:\Users\" & User & "\OneDrive - LEXIQUE\RESEAU LEXIQUE\FACTURE\DEF\" & Format(Now, "yyyy_mm_dd_") & xlSh1.Cells(i, 3) & "_" & xlSh1.Cells(i, 4) & "_" & xlSh1.Cells(i, 6) & ".docx"
Set oDoc = Documents.Add("C:\Users\" & User & "\OneDrive - LEXIQUE\RESEAU LEXIQUE\FACTURE\PROD\Facture_Modèle_Production.docm")
Set oTbl = oDoc.Tables(1)
Set oTb2 = oDoc.Tables(2)
Set oTb3 = oDoc.Tables(3)
oDoc.Bookmarks("S1").Range.Text = xlSh1.Cells(i, 5)
oDoc.Bookmarks("S2").Range.Text = xlSh1.Cells(i, 7)
oDoc.Bookmarks("S3").Range.Text = xlSh1.Cells(i, 6)
oDoc.Bookmarks("S4").Range.Text = xlSh1.Cells(i, 8)
oDoc.Bookmarks("S5").Range.Text = xlSh1.Cells(i, 9)
oDoc.Bookmarks("S6").Range.Text = xlSh1.Cells(i, 10)
oDoc.Bookmarks("S7").Range.Text = xlSh1.Cells(i, 11)
oDoc.Bookmarks("S8").Range.Text = xlSh1.Cells(i, 12)
oDoc.Bookmarks("S9").Range.Text = xlSh1.Cells(i, 13)
oDoc.Bookmarks("S10").Range.Text = xlSh1.Cells(i, 14)
oDoc.Bookmarks("S0").Range.Text = xlSh1.Cells(i, 4)
oDoc.Bookmarks("S21").Range.Text = Format(Now, "dd mmmm yyyy")
oDoc.Bookmarks("SIBAN").Range.Text = xlSh5.Cells(2, 2)
oTbl.Rows.Add
oTbl.Rows.Last.Cells(1).Range.Text = xlSh1.Cells(i, 15)
oTbl.Rows.Last.Cells(1).Range.ParagraphFormat.Alignment = wdAlignParagraphJustify
oTbl.Rows.Last.Cells(2).Range.Text = xlSh1.Cells(i, 18)
oTbl.Rows.Last.Cells(3).Range.Text = xlSh1.Cells(i, 19)
oTbl.Rows.Last.Cells(4).Range.Text = xlSh1.Cells(i, 20)
For k = 2 To 4
oTbl.Rows.Last.Cells(k).Range.ParagraphFormat.Alignment = wdAlignParagraphCenter
Next k
oTbl.Rows.Last.Range.Font.Bold = False
If xlSh1.Cells(i, 21) = 0 Then
oTbl.Rows.Add
oTbl.Rows.Last.Cells(1).Range.Text = "TOTAL"
oTbl.Rows.Last.Cells(2).Range.Text = xlSh1.Cells(i, 24)
oTbl.Rows.Last.Cells(3).Range.Text = xlSh1.Cells(i, 25)
oTbl.Rows.Last.Cells(4).Range.Text = xlSh1.Cells(i, 26)
For k = 2 To 4
oTbl.Rows.Last.Cells(k).Range.ParagraphFormat.Alignment = wdAlignParagraphCenter
Next k
oTb3.Delete
Else
oTbl.Rows.Add
oTbl.Rows.Last.Cells(1).Range.Text = "Frais suivant détail ci-après"
oTbl.Rows.Last.Cells(1).Range.ParagraphFormat.Alignment = wdAlignParagraphJustify
oTbl.Rows.Last.Cells(2).Range.Text = xlSh1.Cells(i, 21)
oTbl.Rows.Last.Cells(3).Range.Text = xlSh1.Cells(i, 22)
oTbl.Rows.Last.Cells(4).Range.Text = xlSh1.Cells(i, 23)
oTbl.Rows.Last.Range.Font.Bold = False
oTbl.Rows.Add
oTbl.Rows.Last.Cells(1).Range.Text = "TOTAL"
oTbl.Rows.Last.Cells(2).Range.Text = xlSh1.Cells(i, 24)
oTbl.Rows.Last.Cells(3).Range.Text = xlSh1.Cells(i, 25)
oTbl.Rows.Last.Cells(4).Range.Text = xlSh1.Cells(i, 26)
For k = 1 To 4
oTbl.Rows.Last.Cells(k).Range.ParagraphFormat.Alignment = wdAlignParagraphCenter
Next k
For j = 2 To jR
If xlSh2.Cells(j, 3) = xlSh1.Cells(i, 4) Then
oTb3.Rows.Add
oTb3.Rows.Last.Cells(1).Range.Text = xlSh2.Cells(j, 12)
oTb3.Rows.Last.Cells(2).Range.Text = xlSh2.Cells(j, 5) & " - " & xlSh2.Cells(j, 6)
oTb3.Rows.Last.Cells(1).Range.ParagraphFormat.Alignment = wdAlignParagraphJustify
oTb3.Rows.Last.Cells(2).Range.ParagraphFormat.Alignment = wdAlignParagraphJustify
oTb3.Rows.Last.Cells(3).Range.Text = xlSh2.Cells(j, 9)
oTb3.Rows.Last.Range.Font.Bold = False
oTb3.Rows.Last.Cells(3).Range.ParagraphFormat.Alignment = wdAlignParagraphCenter
End If
Next j
End If
lR = oDoc.Tables(1).Rows.Count
With oDoc.Tables(1)
For l = 1 To lR
.Rows(l).Height = CentimetersToPoints(1)
.Rows(l).Cells.VerticalAlignment = wdAlignVerticalCenter
Next l
End With
If xlSh1.Cells(i, 21) <> 0 Then
mR = oDoc.Tables(3).Rows.Count
With oDoc.Tables(3)
For m = 1 To mR
.Rows(m).Height = CentimetersToPoints(1)
.Rows(m).Cells.VerticalAlignment = wdAlignVerticalCenter
Next m
End With
End If
oDoc.Tables(1).Rows.Last.Range.Font.Bold = True
If xlSh1.Cells(i, 21) <> 0 Then
oDoc.Tables(3).Rows.Last.Range.Font.Bold = True
End If
oTb2.Columns(1).Cells(2).Range.Text = xlSh1.Cells((i - 1), 24)
oTb2.Columns(2).Cells(2).Range.Text = xlSh1.Cells((i - 1), 27)
oTb2.Columns(3).Cells(2).Range.Text = xlSh1.Cells((i - 1), 25)
With oDoc.Tables(2)
For k = 1 To 2
.Columns(k).Cells(k).Height = CentimetersToPoints(1)
.Rows(k).Cells.VerticalAlignment = wdAlignVerticalCenter
Next k
.Rows.Last.Cells(1).Range.ParagraphFormat.Alignment = wdAlignParagraphJustify
End With
oDoc.SaveAs stDocName
Next i
oDoc.Close
Set oDoc = Nothing
xlWb.Close
xlApp.Quit
Set xlSh1 = Nothing
Set xlSh2 = Nothing
Set xlSh5 = Nothing
Set xlWb = Nothing
Set xlApp = Nothing
End Sub |
Partager