Function TransfertExcelAutomation() Dim xlApp As Excel.Application Dim xlSheet As Excel.Worksheet Dim xlBook As Excel.Workbook Dim I As Long, J As Long Dim t0 As Long, t1 As Long t0 = Timer Dim rec As Recordset Set rec = CurrentDb.OpenRecordset("Clients", dbOpenSnapshot) 'Initialisations Set xlApp = CreateObject("Excel.Application") Set xlBook = xlApp.Workbooks.Add 'Ajouter une feuille de calcul Set xlSheet = xlBook.Worksheets.Add xlSheet.Name = "Tutoriel" ' le titre ' écriture dans la cellule de ligne 1 et de colonne 1 xlSheet.Cells(1, 1) = "Export d'une table Access" ' les entetes ' .Fields(Index).Name renvoie le nom du champ For J = 0 To rec.Fields.Count - 1 xlSheet.Cells(2, J + 1) = rec.Fields(J).Name ' Nous appliquons des enrichissements de format aux cellules With xlSheet.Cells(2, J + 1) .Interior.ColorIndex = 15 .Interior.Pattern = xlSolid .Borders(xlEdgeBottom).LineStyle = xlContinuous .Borders(xlEdgeBottom).Weight = xlThin .Borders(xlEdgeBottom).ColorIndex = xlAutomatic .HorizontalAlignment = xlCenter End With Next J ' recopie des données à partir de la ligne 3 I = 3 Do While Not rec.EOF For J = 0 To rec.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 rec.Fields(J).Type = dbText Then xlSheet.Cells(I, J + 1) = "'" & rec.Fields(J) Else xlSheet.Cells(I, J + 1) = rec.Fields(J) End If Next J I = I + 1 rec.MoveNext Loop ' code de fermeture et libération des objets xlBook.SaveAs "D:\Temp\Feuille.xls" xlApp.Quit rec.Close Set rec = Nothing Set xlSheet = Nothing Set xlBook = Nothing Set xlApp = Nothing t1 = Timer Debug.Print I & " enregistrements", Format(t1 - t0, "0") & " secondes" End Function
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre question