Bonjour tout le monde,

J’améliore actuellement un programme VB.net fonctionnel que j'ai précédemment créé.

L'objectif de la fonction est d'aller piocher les données qui ce trouvent dans un tableau excel lorsque je sélectionne une valeur dans une listbox puis de les afficher via des labels (ci-dessous).



Je me retrouve avec cette erreur :

Une exception de première chance de type 'System.NullReferenceException' s'est produite dans Programme_test.exe

{"Une erreur s'est produite lors de la création du formulaire. Pour plus d'informations, consultez Exception.InnerException. L'erreur est*: La référence d'objet n'est pas définie à une instance d'un objet."}

Partie du code générant l'erreur :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
Imports System
Imports System.IO
Imports System.Text
Imports System.Windows.Forms
Imports System.Drawing
Imports Excel = Microsoft.Office.Interop.Excel
 
Public Class Form1
 
    Dim appXL As Excel.Application
    Dim wbXl As Excel.Workbook
    Dim shXL As Excel.Worksheet
    Dim raXL As Excel.Range
    Dim dest_fichier_Excel As String = "C:\Gestion_des_stocks.xlsx"
 
    Private Sub open_XL()
        appXL = New Excel.Application
        appXL.Visible = True
        wbXl = appXL.Workbooks.Open(Dest_fichier_Excel)
        shXL = wbXl.ActiveSheet
        appXL.DisplayAlerts = False
    End Sub
 
    Function recuperation_tableau()
        Dim i As Integer
        Dim tab As String
 
        i = Excel_chiffre(ListBox1.SelectedItem)
        tab = "B" & i
        tab = shXL.Range(tab).Value
        Label23.Text = shXL.Range(tab).Value ' --- Errreur ---
        tab = "C" & i
        Label33.Text = shXL.Range(tab).Value
        tab = "D" & i
        Label34.Text = shXL.Range(tab).Value
        tab = "E" & i
        Label35.Text = shXL.Range(tab).Value
        tab = "F" & i
        Label36.Text = shXL.Range(tab).Value
        tab = "I" & i
        Label37.Text = shXL.Range(tab).Value
        tab = "J" & i
        Label38.Text = shXL.Range(tab).Value
        tab = "M" & i
        Label39.Text = shXL.Range(tab).Value
        tab = "N" & i
        Label40.Text = shXL.Range(tab).Value
 
        Return vbNull
    End Function
 
    Private Sub close_XL()
        raXL = Nothing
        shXL = Nothing
        wbXl.Close()
        appXL.Quit()
        appXL = Nothing
    End Sub
 
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        open_XL()
        recuperation_tableau()
        'close_XL()
 
 
    End Sub
End Class
L'erreur se produit sur la ligne 31 . Label23.Text = shXL.Range(tab).Value.



Chose que je ne comprend pas, car lorsque je créé un programme test avec le même code sans le reste du programme je n’obtiens plus d'erreurs .

Et si je met :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
 Dim appXL As new Excel.Application
    Dim wbXl As new Excel.Workbook
    Dim shXL As new Excel.Worksheet
    Dim raXL As Excel.Range
Une autre erreur apparaît.


Pouvez-vous m'aider à trouver une solution à ce problème ?

Merci à vous,


teo