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
|
Sub RapportLudo()
'Déclaration des variables
Dim xlApp
Dim xlSheet
Dim xlBook
Dim db As database
Dim qry1 As QueryDef
Dim i As Long, j As Long
Dim t0 As Long, t1 As Long
Dim rec1 As DAO.Recordset
'Implémentation des variables
t0 = Timer
Set db = Application.CurrentDb
Set qry1 = db.QueryDefs("RequêteParamétrée")
qry1.Parameters("PARAM_DATE").Value = Forms!Rapport!Modifiable27
Set rec1 = qry1.OpenRecordset
'Initialisation d'Excel
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Add
'Ajouter une feuille de calcul
Set xlSheet = xlBook.Worksheets.Add
xlSheet.Name = Replace(Date, "/", "-")
xlApp.Visible = True
' les entetes
' .Fields(Index).Name renvoie le nom du champ
For j = 0 To rec1.Fields.Count - 1
xlSheet.Cells(1, j + 1) = rec1.Fields(j).Name
' Nous appliquons des enrichissements de format aux cellules
With xlSheet.Cells(1, j + 1)
.Interior.ColorIndex = 15
.Interior.Pattern = xlSolid
End With
Next j
' recopie des données à partir de la ligne 2
i = 2
Do While Not rec1.EOF
For j = 0 To rec1.Fields.Count - 1
' .Fields(Index).Type renvoie le type du champ
' si c'est un Texte (dbText) nous insérons "'" pour
' qu'il soit reconnu par Excel comme du Texte
If rec1.Fields(j).Type = dbText Then
xlSheet.Cells(i, j + 1) = "'" & rec1.Fields(j)
Else
xlSheet.Cells(i, j + 1) = rec1.Fields(j)
End If
With xlSheet.Cells(i, j + 1)
.Borders(xlEdgeBottom).Weight = xlThin
.Borders(xlEdgeLeft).Weight = xlThin
End With
Next j
i = i + 1
rec1.MoveNext
Loop
' code de fermeture et libération des objets
xlBook.SaveAs "\\RaccordementsAbonnés\RapportAbonnésDu" & Replace(Date, "/", "-") & ".xlsx"
xlApp.Visible = True
rec1.Close
Set rec1 = Nothing
Set xlSheet = Nothing
Set xlBook = Nothing
Set xlApp = Nothing
t1 = Timer
Debug.Print i & " enregistrements", Format(t1 - t0, "0") & " secondes"
End Sub |
Partager