Ecriture vers Excel selection ?
Bonjour à tous,
J'ai un problème que je n'arrive pas à solutionner...
Voici mon code :
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
|
Dim appExcel As Excel.Application 'Application Excel
Dim wbExcel As Excel.Workbook 'Classeur Excel
Dim wsExcel As Excel.Worksheet 'Feuille Excel
On Error GoTo GestErr
'Ouverture de l'application
Set appExcel = CreateObject("Excel.Application")
'Ouverture d'un fichier Excel
If DirectoryExists(chemin & nom_doc) = True Then
Set wbExcel = appExcel.Workbooks.Open(chemin & nom_doc)
Else
appExcel.Workbooks.Add
Set wbExcel = appExcel.ActiveWorkbook
End If
'wsExcel correspond à la première feuille du fichier
Set wsExcel = wbExcel.Worksheets(1)
.....................................................................................
wsExcel.Range("B2:O2").Select
With selection
.....................................................................................
wbExcel.SaveAs (chemin & nom_doc)
wbExcel.Close 'Fermeture du classeur Excel
appExcel.Quit 'Fermeture de l'application Excel
MsgBox "Fichier créé correctement !", vbOKOnly + vbInformation
'Désallocation mémoire
Set wsExcel = Nothing
Set wbExcel = Nothing
Set appExcel = Nothing |
La première fois que j'execute ce code pas de soucy, tout marche correctement et de la facon dont je veux.
Mais si je relance la procedure, alors il me donne l'erreur suivante :
Code:
N°91 : Variable objet ou variable de bloc With non définie
Et avec le Debug je vois que la selection correspond à 'Nothing'
Si je redemarre mon pc alors cela refonctionne....
Avez vous une idée ? Merci pour vos réponses !!