Instanciation d'un objet Classeur Excel
Bonjour tout le monde, voici un sujet maintes fois traité mais je n'ai pas trouvé de solution dans mon cas:
Dans une application WinForms, j'ouvre et je ferme un classeur Excel avec la suite d'instructions suivantes:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| Option Explicit On
Imports Microsoft.Office.Interop.Excel
Imports Windows.Win32.System
Imports Excel = Microsoft.Office.Interop.Excel
Public Class Form1
Public appExcel As Excel.Application 'Application Excel
Public wbExcel As Excel.Workbook 'Classeur Excel
Public wsExcel As Excel.Worksheet 'Feuille Excel
......
appExcel = CreateObject("Excel.Application")
wbExcel = appExcel.Workbooks.Open(NomFichier)
wsExcel = wbExcel.Worksheets("ACCUEIL")
......
appExcel.Visible = True
wbExcel.Save()
wbExcel.Close() 'Fermeture du classeur Excel
appExcel.Quit() 'Fermeture de l'application Excel
appExcel = Nothing
wbExcel = Nothing |
Jusque là, tout va bien.
Maintenant, j'ai ouvert une nouvelle application CONSOLE et j'essaye d'utiliser la même méthode pour travailler avec Excel:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| Option Explicit On
Imports System.Drawing
Imports System.Reflection
Imports Microsoft.Office.Interop.Excel
Imports Excel = Microsoft.Office.Interop.Excel
Module Program
Public appExcel As Excel.Application 'Application Excel
Public wbExcel As Excel.Workbook 'Classeur Excel
Public wsExcel As Excel.Worksheet 'Feuille Excel
.......
Sub Main()
appExcel = CreateObject("Excel.Application")
wbExcel = appExcel.Workbooks.Open(StrInput)
wsExcel = wbExcel.Worksheets("ACCUEIL")
......
wbExcel.Save()
wbExcel.Close() 'Fermeture du classeur Excel
appExcel.Quit() 'Fermeture de l'application Excel |
C'est ici que le problème apparaît: sur l'instruction j'ai l'erreur "System.NullReferenceException:Object Reference not set to an instance of an object".
Je comprends ce qui est écrit mais je cherche comment corriger cette erreur.
D'avance merci de votre aide