Fermer un fichier par VBA
Bonjour à tous,
J'ai un problème pour fermer un fichier .xlsx sous VBA, voici comment est initialisé le programme :
Code:
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
| Private Sub MaSub()
Dim xlPath As String 'xlPath : chemin du fichier Excel
Dim wsName As String 'wsName : nom de la feuille Excel qui contient les données à importer
xlPath = "\Chemin...xlsx"
wsName = "Feuil1"
'déclaration des variables
Dim app As Excel.Application
Dim wkb As Excel.Workbook
Dim wks As Excel.Worksheet
'initialisation des variables
Set app = New Excel.Application
Set wkb = app.Workbooks.Open(xlPath)
Set wks = wkb.Worksheets(wsName)
Dim i As Integer, SQL As String
i = 1
'pour éviter les messages lors de l'ajout des enregistrements
DoCmd.SetWarnings False
With wks
'Traitement des données
End With
'on réactive les messages d'erreurs
DoCmd.SetWarnings True
'libération variables
Set wks = Nothing
Set wkb = Nothing
Set app = Nothing
MsgBox "Import du fichier Excel réussi.", vbInformation + vbOKOnly, "Opération terminée..."
wkb.Close False
End Sub |
J'ai une erreur sur mon avant dernière ligne, si j'enlève cette ligne je n'ai aucun soucis et le code fonctionne, quand j'intègre cette ligne pour fermer le fichier excel mis en jeu, j'ai une erreur : Varaible du bloc With Non Definie. Comment palier à ce problème en sachant qu'elle en fait pas partie du bloc With.
Merci à tous pour vos suggestions
BH