Sub ExempleModeleToXls()
' fichiers modèle et résultat
Const sSourcePathXls As String = "C:\modele.xls"
Const sDestPathXls As String = "C:\sauvegarde.xls"
' déclare et crée l'instance excel
Dim oAppExcel As New Excel.Application
' visible, pas de message, et attache le modèle
With oAppExcel
.Visible = True
.DisplayAlerts = False
.Workbooks.Open FileName:=sSourcePathXls, Editable:=False
End With
' sélection feuille 1, on va remplir la date (C6) et la ville (C7), par exemple
With oAppExcel
.Worksheets("Feuil1").Select
.Cells(6, 3) = CStr(Date)
.Range("C7").Value = "Paris"
End With
' sélection feuille 2, on va colorer une ligne sur 2 (pourquoi pas...)
oAppExcel.Worksheets("Feuil2").Select
Dim i As Integer
For i = 1 To 30
If (i And 1) = 0 Then
oAppExcel.Rows(i).Select
With oAppExcel.Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 255
.TintAndShade = 0
.PatternTintAndShade = 0
End With
End If
Next i
' on sélectionne la première cellule du premier classeur
oAppExcel.Worksheets("Feuil1").Select
oAppExcel.Range("A1").Select
' on enregistre une copie du modèle modifié
oAppExcel.ActiveWorkbook.SaveAs sDestPathXls
' ferme excel et nettoie l'instance
oAppExcel.Application.Quit
Set oAppExcel = Nothing
End Sub