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
| Private Sub Commande63_Click()
If IsNull(Me.cheminaccessexporttransfertpoly) Then
MsgBox "Veuillez indiquer un chemin pour l'exportation", vbExclamation, "ORAGE"
DoCmd.CancelEvent
Else
If IsNull(Me.nomexporttransfertpoly) Or Me.nomexporttransfertpoly = "" Then
MsgBox "Veuillez indiquer un nom de fichier", vbExclamation, "ORAGE"
DoCmd.CancelEvent
Else
If IsNull(Me.debutperiode) Or IsNull(Me.finperiode) Then
MsgBox "Veuillez indiquer une date de début et une date de fin", vbInformation, "ORAGE"
Else
DoCmd.RunMacro "M transfertpoly"
If DCount("*", "T transfertpoly3_Analyse croisée") > 0 Then
Dim xlApp As Object
Dim xlSheet As Object
Dim xlBook As Object
Dim I As Long, J As Long
Dim t0 As Long, t1 As Long
t0 = Timer
Dim rec As Recordset
Dim nbrtot As String
'Compte le nombre d'enregistrement
nbrtot = DCount("*", "T transfertpoly3_Analyse croisée")
Set rec = CurrentDb.OpenRecordset("T transfertpoly3_Analyse croisée", dbOpenSnapshot)
'Initialisations
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Add
'Ajouter une feuille de calcul
Set xlSheet = xlBook.Worksheets.Add
xlBook.Worksheets(Index).Name = "Transfert polyvalence"
' le titre
' écriture dans la cellule de ligne 1 et de colonne 1
xlBook.Worksheets(Index).Cells(2, 1) = "Métier " & Forms![F menu].Form![metieractif]
xlBook.Worksheets(Index).Cells(3, 1) = "TRANSFERT POLYVALENCE DU " & Forms![F MENUTRANSFERT].Form![debutperiode] & " au " & Forms![F MENUTRANSFERT].Form![finperiode]
' les entetes
' .Fields(Index).Name renvoie le nom du champ
For J = 0 To rec.Fields.Count - 1
xlBook.Worksheets(Index).Cells(5, J + 1) = rec.Fields(J).Name
' Nous appliquons des enrichissements de format aux cellules
With xlBook.Worksheets(Index).Cells(2, J + 1)
.Borders(xlEdgeBottom).ColorIndex = xlAutomatic
.HorizontalAlignment = xlLeft
.Font.Bold = True
End With
With xlBook.Worksheets(Index).Cells(5, J + 1)
.Interior.ColorIndex = 15
.Interior.Pattern = xlSolid
.Borders(xlEdgeBottom).LineStyle = xlContinuous
.Borders(xlEdgeBottom).Weight = xlThin
.Borders(xlEdgeBottom).ColorIndex = xlAutomatic
.HorizontalAlignment = xlCenter
.Font.Bold = True
End With
With xlBook.Worksheets(Index).Cells(5 + nbrtot, J + 1)
.Interior.ColorIndex = 3
.Interior.Pattern = xlSolid
.Borders(xlEdgeBottom).LineStyle = xlContinuous
.Borders(xlEdgeBottom).Weight = xlThin
.Borders(xlEdgeBottom).ColorIndex = xlAutomatic
.HorizontalAlignment = xlCenter
.Font.Bold = True
End With
Next J
' recopie des données à partir de la ligne 3
I = 6
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
xlBook.Worksheets(Index).Cells(I, J + 1) = "'" & rec.Fields(J)
Else
xlBook.Worksheets(Index).Cells(I, J + 1) = rec.Fields(J)
End If
Next J
I = I + 1
rec.MoveNext
Loop
xlBook.Worksheets(Index).Columns.AutoFit
'définition du chemin d'exportation
Dim Chemin As String
If Right(Me.cheminaccessexporttransfertpoly, 1) = "\" Then
Chemin = Me.cheminaccessexporttransfertpoly & Me.nomexporttransfertpoly
Else
If Right(Me.cheminaccessexporttransfertpoly, 1) <> "\" Then
Chemin = Me.cheminaccessexporttransfertpoly & "\" & Me.nomexporttransfertpoly
End If
End If
' code de fermeture et libération des objets
xlBook.SaveAs Chemin
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"
MsgBox "Exportation réalisée sur " & Chemin, vbInformation, "Exportation"
Else
If DCount("*", "T transfertpoly3_Analyse croisée") = 0 Then
MsgBox "Il n'y a pas d'enregistrements pour la période choisie", vbExclamation, "Attention"
End If
End If
End If
End If
End If
End Sub |
Partager