IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

VB.NET Discussion :

Référence à Excel


Sujet :

VB.NET

  1. #1
    Membre du Club
    Profil pro
    Inscrit en
    Juillet 2008
    Messages
    175
    Détails du profil
    Informations personnelles :
    Âge : 43
    Localisation : Canada

    Informations forums :
    Inscription : Juillet 2008
    Messages : 175
    Points : 50
    Points
    50
    Par défaut Référence à Excel
    Bonjour tout le monde,
    j'ai un message d'erreur dans application vb.net qui me bloque depuis plusieurs jours, j'ai essaye de le regler sans aucun succes.
    System.NullReferenceException:
    La référence d'objet n'est pas définie à une instance d'un objet. à PoMailPh.frmPrinc.fct_fill_excel(String[] lines, String sqltype) à PoMailPh.frmPrinc.procVerifFichier().
    Voici les 2 fonctions :
    Code vb.Net : 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
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
     Private Sub fct_fill_excel(ByVal lines() As String, ByVal sqltype As String)
            Dim line_0() As String
            Try
     
                Dim line_1() As String
                Dim line_2() As String
                Dim line_3() As String
                Dim line_x() As String
                Dim i As Integer
                Dim strsql As String
                Dim qty As Decimal
                Dim S1, ErrStr As String
                Dim po_xl As New Excel.Application
                Dim po_book As Excel.Workbook
                Dim po_sheet As Excel.Worksheet
     
                If sqltype = "PO" Or sqltype = "MA" Then
     
                    'lines(0)
                    line_0 = Split(Replace(Replace(lines(0), Chr(34), ""), "'", " "), ";")
     
                    'lines(1)
                    line_1 = Split(Replace(Replace(lines(1), Chr(34), ""), "'", " "), ";")
     
                    'lines(2)
                    line_2 = Split(Replace(Replace(lines(2), Chr(34), ""), "'", " "), ";")
     
                    'lines(3)
                    line_3 = Split(Replace(Replace(lines(3), Chr(34), ""), "'", " "), ";")
     
                    'Création des settings pour la gestion du po dans excel
                    po_xl = New Excel.Application
     
                    If line_1(1) = "E" Then
                        po_book = po_xl.Workbooks.Open(Filename:=layout_e)
                        'po_xl.Workbooks.Open(Filename:=layout_e)
                    Else
                        po_book = po_xl.Workbooks.Open(Filename:=layout_f)
                        'po_xl.Workbooks.Open(Filename:=layout_f)
                    End If
                    po_sheet = po_book.ActiveSheet
                    po_xl.Visible = False
     
                    'Création de l'entête du PO
                    'to
                    If line_3(1) <> "" And line_3(2) <> "" Then
                        po_sheet.Cells(2, 7) = line_2(0).ToUpper
                        po_sheet.Cells(3, 7) = line_3(0).ToUpper
                        po_sheet.Cells(4, 7) = line_3(1).ToUpper
                        po_sheet.Cells(5, 7) = line_3(2).ToUpper
                        po_sheet.Cells(6, 7) = line_3(3).ToUpper & ", " & line_3(4).ToUpper & "  " & line_3(5).ToUpper
                        po_sheet.Cells(7, 7) = line_3(6).ToUpper
                    ElseIf line_3(1) = "" And line_3(2) = "" Then
                        po_sheet.Cells(2, 7) = line_2(0).ToUpper
                        po_sheet.Cells(3, 7) = line_3(0).ToUpper
                        po_sheet.Cells(4, 7) = line_3(3).ToUpper & ", " & line_3(4).ToUpper & "  " & line_3(5).ToUpper
                        po_sheet.Cells(7, 7) = line_3(6).ToUpper
                    ElseIf line_3(1) <> "" And line_3(2) = "" Then
                        po_sheet.Cells(2, 7) = line_2(0).ToUpper
                        po_sheet.Cells(3, 7) = line_3(0).ToUpper
                        po_sheet.Cells(4, 7) = line_3(1).ToUpper
                        po_sheet.Cells(5, 7) = line_3(3).ToUpper & ", " & line_3(4).ToUpper & "  " & line_3(5).ToUpper
                        po_sheet.Cells(7, 7) = line_3(6).ToUpper
                    ElseIf line_3(1) = "" And line_3(2) <> "" Then
                        po_sheet.Cells(2, 7) = line_2(0).ToUpper
                        po_sheet.Cells(3, 7) = line_3(0).ToUpper
                        po_sheet.Cells(4, 7) = line_3(2).ToUpper
                        po_sheet.Cells(5, 7) = line_3(3).ToUpper & ", " & line_3(4).ToUpper & "  " & line_3(5).ToUpper
                        po_sheet.Cells(7, 7) = line_3(6).ToUpper
                    End If
     
                    'ship to
                    If line_2(6) <> "" And line_2(7) <> "" Then
                        po_sheet.Cells(2, 15) = line_2(21).ToUpper
                        po_sheet.Cells(3, 15) = line_2(14).ToUpper
                        po_sheet.Cells(4, 15) = line_2(6).ToUpper
                        po_sheet.Cells(5, 15) = line_2(7).ToUpper
                        po_sheet.Cells(6, 15) = line_2(8).ToUpper & ", " & line_2(9).ToUpper & "  " & line_2(10).ToUpper
                        po_sheet.Cells(7, 15) = line_2(12).ToUpper
                    ElseIf line_2(6) = "" And line_2(7) = "" Then
                        po_sheet.Cells(2, 15) = line_2(21).ToUpper
                        po_sheet.Cells(3, 15) = line_2(14).ToUpper
                        po_sheet.Cells(4, 15) = line_2(8).ToUpper & ", " & line_2(9).ToUpper & "  " & line_2(10).ToUpper
                        po_sheet.Cells(7, 15) = line_2(12).ToUpper
                    ElseIf line_2(6) <> "" And line_2(7) = "" Then
                        po_sheet.Cells(2, 15) = line_2(21).ToUpper
                        po_sheet.Cells(3, 15) = line_2(14).ToUpper
                        po_sheet.Cells(4, 15) = line_2(6).ToUpper
                        po_sheet.Cells(5, 15) = line_2(8).ToUpper & ", " & line_2(9).ToUpper & "  " & line_2(10).ToUpper
                        po_sheet.Cells(7, 15) = line_2(12).ToUpper
                    ElseIf line_2(6) = "" And line_2(7) <> "" Then
                        po_sheet.Cells(2, 15) = line_2(21).ToUpper
                        po_sheet.Cells(3, 15) = line_2(14).ToUpper
                        po_sheet.Cells(4, 15) = line_2(7).ToUpper
                        po_sheet.Cells(5, 15) = line_2(8).ToUpper & ", " & line_2(9).ToUpper & "  " & line_2(10).ToUpper
                        po_sheet.Cells(7, 15) = line_2(12).ToUpper
                    End If
                    '# cmd
                    po_sheet.Cells(10, 1) = line_2(1).ToUpper
                    'Date cmd
                    If line_1(1) = "E" Then
                        po_sheet.Cells(10, 5) = line_2(2).Substring(3, 2) & "/" & line_2(2).Substring(6, 2) & "/" & line_2(2).Substring(0, 2)
                    Else
                        po_sheet.Cells(10, 5) = line_2(2).Substring(6, 2) & "/" & line_2(2).Substring(3, 2) & "/" & line_2(2).Substring(0, 2)
                    End If
                    'Date livrais
                    If line_1(1) = "E" Then
                        po_sheet.Cells(10, 6) = line_2(3).Substring(3, 2) & "/" & line_2(3).Substring(6, 2) & "/" & line_2(3).Substring(0, 2)
                    Else
                        po_sheet.Cells(10, 6) = line_2(3).Substring(6, 2) & "/" & line_2(3).Substring(3, 2) & "/" & line_2(3).Substring(0, 2)
                    End If
                    'Ship by
                    po_sheet.Cells(10, 7) = line_2(17).ToUpper
                    'FOB
                    po_sheet.Cells(10, 10) = line_2(13).ToUpper
                    'tx
                    po_sheet.Cells(9, 16) = line_2(19).ToUpper
                    po_sheet.Cells(10, 16) = line_2(20).ToUpper
                    '# fact
                    po_sheet.Cells(12, 1) = line_2(4).ToUpper
                    'Buyer
                    po_sheet.Cells(12, 5) = line_2(12).ToUpper
                    'Terms
                    po_sheet.Cells(12, 8) = line_2(5).ToUpper
                    'Freight
                    po_sheet.Cells(12, 11) = line_2(11).ToUpper
     
                    'Création des lignes de PO
                    For i = 4 To lines.GetUpperBound(0) - 1
                        line_x = Split(Replace(Replace(lines(i), Chr(34), ""), "'", " "), ";")
                        qty = qty + line_x(1)
                        'line
                        po_sheet.Cells(12 + i, 1) = line_x(0).ToUpper
                        'qte
                        po_sheet.Cells(12 + i, 2) = line_x(1).ToUpper
                        'vendor-item
                        If line_x(2) = "" Then
                            po_sheet.Cells(12 + i, 4) = line_x(4).ToUpper
                        Else
                            po_sheet.Cells(12 + i, 4) = line_x(2).ToUpper
                        End If
                        'desc
                        po_sheet.Cells(12 + i, 6) = line_x(3).ToUpper
                        'item-number
                        po_sheet.Cells(12 + i, 11) = "#PHV : " & line_x(4).ToUpper
                        'udm
                        po_sheet.Cells(12 + i, 15) = line_x(5).ToUpper
                        'prix
                        po_sheet.Cells(12 + i, 16) = CDec(line_x(6))
                        'amount
                        po_sheet.Cells(12 + i, 17) = CDec(line_x(7))
                    Next
                    po_sheet.Cells(12 + i + 1, 4) = line_x(10).ToUpper & line_x(11).ToUpper & line_x(12).ToUpper & line_x(13).ToUpper & line_x(14).ToUpper & line_x(15).ToUpper & line_x(16).ToUpper & line_x(17).ToUpper & line_x(18).ToUpper & line_x(19).ToUpper & line_x(20).ToUpper & line_x(21).ToUpper & line_x(22).ToUpper & line_x(23).ToUpper
                    po_sheet.Cells(12 + i + 3, 2) = "________"
                    po_sheet.Cells(12 + i + 3, 17) = "___________"
                    If line_1(1) = "E" Then
                        If line_2(15) = "USD" Then
                            po_sheet.Cells(12 + i + 3, 5) = "****** AMOUNTS SPECIFIED IN U.S.A CURRENCY ******"
                        Else
                            po_sheet.Cells(12 + i + 3, 5) = "******* AMOUNTS SPECIFIED IN CDN CURRENCY *******"
                        End If
                    Else
                        If line_2(15) = "USD" Then
                            po_sheet.Cells(12 + i + 3, 5) = "****** MONTANTS SPÉCIFIÉS EN DEVISE CDN ******"
                        Else
                            po_sheet.Cells(12 + i + 3, 5) = "****** MONTANTS SPÉCIFIÉS EN DEVISE USA ******"
                        End If
                    End If
                    po_sheet.Cells(12 + i + 4, 2) = qty
                    po_sheet.Cells(12 + i + 4, 16) = "TOTAL : "
                    po_sheet.Cells(12 + i + 4, 17) = CDec(line_2(16))
                    If File.Exists(f_out & "po_" & line_0(1) & ".xls") Then
                        File.Delete(f_out & "po_" & line_0(1) & ".xls")
                    End If
                    If line_0(0) = "@" Then
                        po_sheet.SaveAs(f_out & "ma_" & line_0(1) & ".xls")
                    Else
                        po_sheet.SaveAs(f_out & "po_" & line_0(1) & ".xls")
                    End If
                    po_xl.Quit()
                    po_sheet = Nothing
                    po_book = Nothing
                    po_xl = Nothing
     
                    'Kill du process excel
                    Dim Processes As Process() = Nothing
                    Processes = Process.GetProcessesByName("EXCEL")
                    ' Load ID Processes in Array
                    Dim intProcesses(Processes.GetUpperBound(0)) As Int16
                    Dim j As Int16
                    For j = 0 To Processes.GetUpperBound(0)
                        Process.GetProcessById(CInt(Processes(j).Id.ToString)).Kill()
                    Next
                    'Envoi du email si tel est le cas
                    If line_0(0) = "@" Then
                        fct_wait_sec(60000)
                        fct_send_mail(envoyeur, line_1(0), "Purchase Order/Commande d'Achat # " & line_0(1), 1, "<font face=arial size=2 color='black'>Purchase Order/Commande d'Achat # " & line_0(1) & "</font><br><br><img src='" & signature & "'><br><br><b><font face=arial size=2 color='#aaaaaa'>DISCLAIMER: </b><BR>This communication is for use by the intended recipient and contains information that may be privileged, confidential or copyrighted under applicable law. If you are not the intended recipient, you are hereby formally notified that any use, copying or distribution of this e-mail, in whole or in part, is strictly prohibited. Please notify the sender by return e-mail from your system.</font>", "ma_" & line_0(1))
                    End If
                End If
            Catch ex As Exception
                fct_send_mail(envoyeur, warning, "ERREUR PO VIA MAIL " & line_0(1), 0, ex.ToString, "none")
                '# erreur no 8
                fct_exec_sql("UPDATE pur_order SET err=8 WHERE pur_order.po_number='" & line_0(1) & "'")
            End Try
        End Sub
     
     
     
     
     
     
        Private Sub procVerifFichier()
            Dim i As Integer
            Dim sFiles() As String
            Dim FreeF As Integer
            Dim str() As String
            Dim tmp() As String
     
            Do
                Try
                    'System.Threading.Thread.Sleep(5000)
                    If Not File.Exists(f_in & "\pomail.lock") Then
                        'pour avoir les noms des fichiers et des sous-répertoires
                        'fso = Createobject("Scripting.FileSystem")
                        'f = fso.GetFile(f_in)
                        sFiles = Directory.GetFiles(f_in)
                        For i = 0 To sFiles.GetUpperBound(0)
     
                            If InStr(sFiles(i), ".txt") > 0 Then
     
                                'System.Threading.Thread.Sleep(5000)
                                FreeF = FreeFile()                                            'Possibilité de mettre 1************
                                FileOpen(FreeF, sFiles(i), OpenMode.Input)                   'Ouverture du fichier créé**********
                                str = Split(InputString(FreeF, FileLen(sFiles(i))), Chr(10)) 'Séparation des lignes du fichier***
                                FileClose(FreeF)
                                File.Delete(archive & sFiles(i).Substring(27, sFiles(i).Length - 27))
                                File.Move(sFiles(i), archive & sFiles(i).Substring(27, sFiles(i).Length - 27))
                                'MsgBox(sFiles(i), archive & sFiles(i))
     
                                With txtwatch
     
                                    .SelectionStart = Len(txtwatch.Text)
                                    .SelectionProtected = False
                                    .SelectionStart = Len(txtwatch.Text) + 1
                                    .SelectionColor = System.Drawing.Color.Red
                                    .SelectedText = "Fichier créé : " & Replace(sFiles(i), f_in, "") & " " & Date.Now & vbCrLf
                                    .SelectionProtected = True
                                    .Focus()
                                    .SelectionStart = txtwatch.Text.Length
                                    .ScrollToCaret()
                                End With
                                tmp = Split(Replace(Replace(str(0), Chr(34), ""), "'", " "), ";")
                                'If tmp(2).ToUpper <> "V" Then
                                'Exit Sub
                                'end if
                                If tmp(2).ToUpper = "V" Then
                                    fct_fill_sql(str, sFiles(i).Substring(27, 2).ToUpper)
                                    fct_fill_excel(str, sFiles(i).Substring(27, 2).ToUpper)
                                End If
                            End If
                            If File.Exists(f_in & "\pomail.lock") Then Exit For
                        Next
                    End If
     
                Catch ex As Exception
                    fct_send_mail(envoyeur, warning, "ERREUR PO VIA MAIL " & Replace(sFiles(i), f_in, ""), 0, ex.ToString, "none")
                    '# erreur no 1
                    fct_exec_sql("UPDATE pur_order SET err=1 WHERE pur_order.po_number='0'")
     
                End Try
                Exit Do
            Loop
        End Sub

    J'espere regler ce probleme avec vos comptences

    Merci beaucoup d'avance

  2. #2
    Expert éminent sénior Avatar de Pol63
    Homme Profil pro
    .NET / SQL SERVER
    Inscrit en
    Avril 2007
    Messages
    14 177
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 42
    Localisation : France, Puy de Dôme (Auvergne)

    Informations professionnelles :
    Activité : .NET / SQL SERVER

    Informations forums :
    Inscription : Avril 2007
    Messages : 14 177
    Points : 25 125
    Points
    25 125
    Par défaut
    autant mettre le code sur le forum, il y a une balise pour la mise en forme (bouton #)

    si tu testes en mode debug, ca doit te donner le numéro de ligne de l'erreur
    et le nullreferenceexception (plus communément appelée erreur du débutant) veut dire que tu utilises une variable non initialisée


    par exemple
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
     
    dim b as button
    b.Text = ""
    ici b vaut nothing donc b.Text équivaut à nothing.Text, qui n'existe pas
    new sert à instancier (dim b as new button)

  3. #3
    Membre du Club
    Profil pro
    Inscrit en
    Juillet 2008
    Messages
    175
    Détails du profil
    Informations personnelles :
    Âge : 43
    Localisation : Canada

    Informations forums :
    Inscription : Juillet 2008
    Messages : 175
    Points : 50
    Points
    50
    Par défaut instanciation des objets
    merci pour ta reponse,
    quand je compile en mode debug et j'ai aucune erreur, mais je recois ce message des fois parcke l'application est en fonction.
    j'ai su d'apres quelques recherche qu'il faut absolument installer la framework chez les utlisateurs.
    l'application roule tres bien mais des fois envoie des messages d'erreurs.
    je sais pas si le bug vient de la???

  4. #4
    Membre expérimenté
    Avatar de FRED.G
    Profil pro
    Inscrit en
    Novembre 2002
    Messages
    1 032
    Détails du profil
    Informations personnelles :
    Âge : 44
    Localisation : France

    Informations forums :
    Inscription : Novembre 2002
    Messages : 1 032
    Points : 1 505
    Points
    1 505
    Par défaut
    Place un point d'arrêt et exécute ton code pas à pas en appuyant sur F8.

    Je te conseille de lire ce cours pour apprendre à déboguer : http://plasserre.developpez.com/v4-200.htm

  5. #5
    Membre du Club
    Profil pro
    Inscrit en
    Juillet 2008
    Messages
    175
    Détails du profil
    Informations personnelles :
    Âge : 43
    Localisation : Canada

    Informations forums :
    Inscription : Juillet 2008
    Messages : 175
    Points : 50
    Points
    50
    Par défaut Instanciation des objets; débogage
    Bonjour,
    j'ai mis des points d'arret sur la fonction qui genere cette erreur et j'ai constate qu'il ya des sous messages d'erreurs:

    Le composant référencé 'Excel' est introuvable.
    Type 'Excel.Application' non défini.
    Type 'Excel.Workbook' non défini.
    Type 'Excel.Worksheet' non défini.

    j'ai ajoute la reference Excel au projet, mais ca donnait aucun changement

  6. #6
    Membre expérimenté
    Avatar de FRED.G
    Profil pro
    Inscrit en
    Novembre 2002
    Messages
    1 032
    Détails du profil
    Informations personnelles :
    Âge : 44
    Localisation : France

    Informations forums :
    Inscription : Novembre 2002
    Messages : 1 032
    Points : 1 505
    Points
    1 505
    Par défaut
    Tu as ici un exemple de programmation d'excel en .NET : http://dotnet.developpez.com/cours/office/

  7. #7
    Membre du Club
    Profil pro
    Inscrit en
    Juillet 2008
    Messages
    175
    Détails du profil
    Informations personnelles :
    Âge : 43
    Localisation : Canada

    Informations forums :
    Inscription : Juillet 2008
    Messages : 175
    Points : 50
    Points
    50
    Par défaut Instanciation des objets; débogage
    salut,
    le probleme est que le systeme ne trouve pas la reference Microsoft excel 11.0, je l'ai supprime et je lai reimporte et j'ai meme fait une mise a jour de l'excel mais maleheureuseument toujours le meme probleme
    s'il des soltutions ou des idees, je suis preneur.
    Merci beaucoup

  8. #8
    Membre du Club
    Profil pro
    Inscrit en
    Juillet 2008
    Messages
    175
    Détails du profil
    Informations personnelles :
    Âge : 43
    Localisation : Canada

    Informations forums :
    Inscription : Juillet 2008
    Messages : 175
    Points : 50
    Points
    50
    Par défaut Instanciation des objets; débogage
    c'est bon, ca marche
    j'ai ajouté Microsoft.Office.Interop.Excel.dll,dans bin dans le dossier du projet..
    aprés j'ai ajouter la reference.dans projet/ajouter une reférence/parcourir..et j'ai fait rentre le chemin où le fichier Microsoft.Office.Interop.Excel.dll se trouve (bin)
    Merci beaucoup

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Référence Excel selon version
    Par helje dans le forum VBA Access
    Réponses: 4
    Dernier message: 04/07/2008, 11h11
  2. Référence Excel 2003
    Par charliejo dans le forum ASP.NET
    Réponses: 20
    Dernier message: 22/05/2008, 15h22
  3. Réponses: 2
    Dernier message: 11/09/2007, 23h07
  4. [VB.NET] Importation Référence Excel
    Par HULK dans le forum Windows Forms
    Réponses: 2
    Dernier message: 05/12/2006, 16h44
  5. Référence Excel depuis Access
    Par Guiguouz dans le forum Access
    Réponses: 1
    Dernier message: 08/06/2006, 14h18

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo