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
|
Public Sub export2Excel(sTemplate As String, sSheetName As String, sPath2Export As String, sFileName2Export As String)
Dim oExcel As New Excel.Application
Dim WB As New Excel.workbook
Dim WS As Excel.Worksheet
Dim rng As Excel.Range
Dim rst As DAO.Recordset
Dim sSQL As String
sSQL = "SELECT * FROM TA_TABLE"
With oExcel
.Visible = False
Set WB = .Workbooks.Add(sTemplate) 'ton chemin vers le template excel
With WB
Set WS = WB.Worksheets("Nom de ta feuille template")
With WS
Set rst = CurrentDb.OpenRecordset(sSQL, dbOpenForwardOnly, dbReadOnly)
Set rng = .Range("C5")
rng.CopyFromRecordset rst
rst.Close
'tu peux formater ta sortie excel ici pour faire jolie...
.Name = sSheetName 'tu renommes la feuille
End With
.SaveAs FileName:=sPath2Export & sFileName2Export, FileFormat:=52
End With
.QUIT
End With
Set WB = Nothing
Set WS = Nothing
Set rst = Nothing
Set rng = Nothing
End Sub |
Partager