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
|
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
Dim Nom As String
Dim Rep As Variant
t0 = Timer
Dim rec As Recordset
Dim rec2 As Recordset
Set rec2 = CurrentDb.OpenRecordset("DERNIERS", dbOpenSnapshot)
Set rec = CurrentDb.OpenRecordset("DISTINCT_PREMIERS", dbOpenSnapshot)
'Initialisations
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Add
'Ajouter une feuille de calcul
Set xlSheet = xlBook.Worksheets.Add
xlSheet.Name = "livraison"
' recopie des données à partir de la ligne 1
I = 1
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
' recopie des données à partir de la ligne 2
I = 2
Do While Not rec2.EOF
For J = 0 To rec2.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 rec2.Fields(J).Type = dbText Then
xlSheet.Cells(I, J + 1) = "'" & rec2.Fields(J)
Else
xlSheet.Cells(I, J + 1) = rec2.Fields(J)
End If
Next J
I = I + 1
rec2.MoveNext
Loop
With ActiveWorkbook
Nom = .Name
If .Path <> "" Then
Nom = Left$(Nom, InStr(1, Nom, ".") - 1)
End If
End With
' code de fermeture et libération des objets
xlBook.SaveAs "C:\Documents and Settings\Administrateur.DOMAVIATEC\Bureau\Feuille.xls"
Rep = xlApp.GetSaveAsFilename("mon_fichier", "Fichier CSV,*.csv")
If Rep <> False Then
c = MsgBox("ok")
End If
xlApp.Quit
rec.Close
rec2.Close
Set rec = Nothing
Set rec2 = Nothing
Set xlSheet = Nothing
Set xlBook = Nothing
Set xlApp = Nothing
t1 = Timer
Debug.Print I & " enregistrements", Format(t1 - t0, "0") & " secondes"
End Function |
Partager