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 :

Piloter Excel depuis vb.net


Sujet :

VB.NET

  1. #1
    Membre confirmé
    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
    Par défaut Piloter Excel depuis vb.net
    bonjour tout le monde,
    jai un petit souci au niveau du code vb.net qui gere excel, par exemple quand j'ecris:
    dim xls as excel.application
    set xls = new excel.application
    le set disparait automatiquement quand je passe a l'instruction suivante.
    quelqu'un a une idee s'il vous plait??

  2. #2
    Expert confirmé

    Avatar de Philippe Vialatte
    Homme Profil pro
    Architecte technique
    Inscrit en
    Juillet 2004
    Messages
    3 029
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 46
    Localisation : France, Alpes Maritimes (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Architecte technique
    Secteur : High Tech - Produits et services télécom et Internet

    Informations forums :
    Inscription : Juillet 2004
    Messages : 3 029
    Par défaut
    euh...oui, oublie le set, tu n'es pas en vba

    Mon Blog

    The Cake is still a lie !!!



    Vous voulez contribuer à la rubrique .NET ? Contactez-moi par MP.
    Vous voulez rédiger des articles pour la rubrique .NET ? Voici la procédure à suivre.

  3. #3
    Membre confirmé
    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
    Par défaut Piloter Excel depuis vb.net
    Merci pour ta reponse,
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
     
    Dim ma As New Excel.Application
    Dim ca As Excel.Workbook
    Dim da As Excel.Worksheet
    ca = ma.Workbooks.Open(Filename:=layout_e)
    ca = ma.Workbooks.Open(Filename:=layout_f)
    da = ma.ActiveSheet
    ma.Visible = False
    Quesque je dois changer s'il vout plait sur ces lignes de gestion d'objet excel sur vb.net pour avoir de la paix de ce message d'erreur:
    System.NullReferenceException: La référence d'objet n'est pas définie à une instance d'un objet.
    d'apres moi le new est suppose de regler ce probleme, mais j'ai l'impression que non parcque je recois ce messages plusieurs fois par jour

    Merci beaucoup pour toute reponse.

  4. #4
    Expert confirmé

    Avatar de Philippe Vialatte
    Homme Profil pro
    Architecte technique
    Inscrit en
    Juillet 2004
    Messages
    3 029
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 46
    Localisation : France, Alpes Maritimes (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Architecte technique
    Secteur : High Tech - Produits et services télécom et Internet

    Informations forums :
    Inscription : Juillet 2004
    Messages : 3 029
    Par défaut
    Sur quelle instruction as-tu l'erreur ???

    Mon Blog

    The Cake is still a lie !!!



    Vous voulez contribuer à la rubrique .NET ? Contactez-moi par MP.
    Vous voulez rédiger des articles pour la rubrique .NET ? Voici la procédure à suivre.

  5. #5
    Membre confirmé
    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
    Par défaut Piloter Excel depuis vb.net
    ces instructions font partie du code d'une fonction qui genere ce message d'erreur et malheureusement je suis pas capable de deboguer l'application pour avoir la ligne qui declenche l'erreur parcke c'est la premiere fois que je travaille avec visual studio.
    j'ai essaye de le faire, mais l'application s'arrete pas sur les breakpoints que j'ai defini sur le code.
    je vais mettre ma focntion avec le message d'erreur au complet et tu peux la deboguer si tu as un peu de temps stp
    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
    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
     
     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
                'po_xl = GetObject(, "Excel.Application")
     
                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), ""), "'", " "), ";")
                    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_xl.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))
                    po_xl.DisplayAlerts = False
                    If line_0(0) = "@" Then
     
                        If File.Exists(f_out & "ma_" & line_0(1) & ".xls") Then
                            File.Delete(f_out & "ma_" & line_0(1) & ".xls")
                        End If
     
                        po_sheet.SaveAs(f_out & "ma_" & line_0(1) & ".xls")
                    Else
     
                        If File.Exists(f_out & "po_" & line_0(1) & ".xls") Then
                            File.Delete(f_out & "po_" & line_0(1) & ".xls")
                        End If
     
                        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
    et le message d'erreur est celui la:
    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()

    dans la fonction procVerifFichier(), je fais appel a la fonction fct_fill_excel.
    Merci beaucoup pour ton aide.

  6. #6
    Membre confirmé
    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
    Par défaut Piloter Excel depuis vb.net
    je pense que j'ai trouve les 2 lignes qui provoquent l'erreur grace a la pile des appels:

    po_sheet.Cells(12 + i, 4) = line_x(4).ToUpper
    po_sheet.Cells(12 + i, 4) = line_x(2).ToUpper

    Toute solution va etre bien apprecie.
    Merci beacoup

  7. #7
    Membre confirmé
    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
    Par défaut Piloter Excel depuis vb.net
    Bonjour chers amis,
    j'ai reussi a regler le probleme de la reference d'objet en mettant l'instruction suivante: po_xl = New Excel.Application avant la creation des lignes du fichier excel et ca marche parfaitement

    et la j'ai un message d'erreur qui a un rapport avec application excel aussi:
    System.Runtime.InteropServices.COMException (0x80080005): La récupération de la fabrique de classe COM pour le composant avec le CLSID {00024500-0000-0000-C000-000000000046} a échoué en raison de l'erreur suivante : 80080005.

    Merci pour l'aide

  8. #8
    Membre confirmé
    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
    Par défaut Piloter Excel depuis vb.net
    j'ai remplace po_xl = New Excel.Application par po_xl = CreatObject("Excel.Application") et ca marchait.
    Merci quand meme

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

Discussions similaires

  1. [Toutes versions] Piloter Excel depuis une autre application : Msgbox
    Par damsmut dans le forum Macros et VBA Excel
    Réponses: 10
    Dernier message: 19/05/2010, 11h38
  2. piloter excel depuis access : trier
    Par Arzawe dans le forum VBA Access
    Réponses: 2
    Dernier message: 28/03/2008, 11h29
  3. piloter excel depuis un site ASP.NET
    Par sergio94 dans le forum Général Dotnet
    Réponses: 4
    Dernier message: 22/01/2008, 15h25
  4. Piloter Excel depuis Forms
    Par Invité dans le forum Forms
    Réponses: 3
    Dernier message: 29/08/2007, 08h19
  5. [VB.Net] - Acces à Excel depuis VB.Net
    Par toninonino dans le forum Windows Forms
    Réponses: 14
    Dernier message: 30/09/2005, 11h12

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