Visual Basic - Excepción no controlada del tipo 'System.Runtime.InteropServices.COMException'

Life is soft - evento anual de software empresarial
 
Vista:

Excepción no controlada del tipo 'System.Runtime.InteropServices.COMException'

Publicado por Nico (1 intervención) el 29/03/2017 12:52:22
Hola,

Ante todo quiero pedir disculpas porque el código que pasteare es bastante vergonzoso... Llevo poco tiempo programando y este programa no está muy limpio. Pero bueno, al tema, estoy tratando de realizar un chequeo entre dos archivos. Uno de estos archivos es un Excel, y hasta ahora me había estado funcionando "bien". Hoy, con un nuevo archivo Excel que ha llegado al trabajo (y tras las modificaciones pertinentes en las celdas) está generándose la siguiente excepción:

Excepción no controlada del tipo 'System.Runtime.InteropServices.COMException' en Program.exe

Información adicional: El índice no es válido. (Excepción de HRESULT: 0x8002000B (DISP_E_BADINDEX))

Según he podido leer en google... pueden haber varias razones. Una que el idioma entre Excel y windows sea diferente (no es el caso). Otra que el indice utilizado exceda el rango posible (no digo que no sea esta la razón, pero en mi código no encuentro donde podría estar el error que me sacara fuera de rango). Y repito, este programa estaba funcionando "bien" hasta hoy. Pensé que quizás era el formato del excel *.xlsm, pero probé a copiar el contenido a un archivo *.xlsx, que es del mismo tipo que el archivo que había estado funcionando hasta ahora. Agradecería algo de luz... porque no encuentro la solución. A continuación el código, disculpas de nuevo...

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
Imports Excel = Microsoft.Office.Interop.Excel
Public Class TimerCheck
    Private Sub Abrir_Click(sender As Object, e As EventArgs) Handles Abrir.Click
        Dim filepath As String
        If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
            'Abrir archivo
            filepath = OpenFileDialog1.FileName()
        End If
 
        DataGridView1.Rows.Clear()
 
        'Almacenar la información del archivo en un String'
        Dim fileInfoText As String = ManejarArchivoTimer(filepath)
    End Sub
 
    Function ManejarArchivoTimer(ByVal filepath As String) As String
        'Comprobar que el archivo existe'
        If My.Computer.FileSystem.FileExists(filepath) = False Then
            Throw New Exception("File not found: " & filepath)
        End If
 
        'Obtener la infomación del archivo'
        Dim thisfile As System.IO.FileInfo = My.Computer.FileSystem.GetFileInfo(filepath)
        Dim Linea, Timer, Thickness As String
        Dim SpotName, Index, PrimCar, type, test As String
        Dim PosIndex As Integer
 
        'Abrir el nuevo archivo'
        Dim sr As System.IO.StreamReader = My.Computer.FileSystem.OpenTextFileReader(filepath)
 
        While (sr.Peek() >= 0)
            Linea = sr.ReadLine()
            If Not Linea.Contains("1-18") And Not Linea.Contains("1-00") And Not Linea.Contains("Spot Reference") And Not Linea.Contains("#") And Not Linea.Contains("2-00") And Not Linea.Contains("Punkt-Zuordnung") Then
                PrimCar = Mid(Linea, 1, 1)
                If (PrimCar.Contains("1")) Then
                    type = Mid(Linea, 1, 2)
                    test = Mid(Linea, 9, 11)
                    Timer = Trim(test)
                    test = Mid(Linea, 189, 11)
                    SpotName = Trim(test)
                    PosIndex = InStr(1, Linea, "-6")
                    Index = Mid(Linea, PosIndex + 1, 4)
                    test = Mid(Linea, 102, 14)
                    Thickness = Trim(test)
                End If
 
                If (PrimCar.Contains("5")) Then
                    type = Mid(Linea, 1, 1)
                    test = Mid(Linea, 8, 11)
                    Timer = Trim(test)
                    test = Mid(Linea, 189, 11)
                    SpotName = Trim(test)
                    PosIndex = InStr(1, Linea, "-6")
                    test = Mid(Linea, PosIndex + 1, 4)
                    Index = Trim(test)
                    test = Mid(Linea, 103, 14)
                    Thickness = Trim(test)
                End If
 
                DataGridView1.AllowUserToAddRows = False
                'Almacenamos los datos en el datagrid'
                DataGridView1.Rows.Add(Timer, SpotName, type, Index, Thickness)
 
            End If
        End While
        sr.Close()
    End Function
 
    Private Sub Abrir1_Click(sender As Object, e As EventArgs) Handles Abrir1.Click
        Dim filepath As String
        filepath = ""
        If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
            'Abrir archivo
            filepath = OpenFileDialog1.FileName()
        End If
 
        DataGridView2.Rows.Clear()
 
        'Almacenar la información del archivo en un String'
        Dim fileInfoText As String = ManejarArchivoCross(filepath)
    End Sub
 
    Function ManejarArchivoCross(ByVal filepath As String) As String
        Dim app As New Excel.Application
        Dim worksheet As Excel.Worksheet
        Dim workbook As Excel.Workbook
 
        Dim timer, spotname, typeid, index, thickness, test As String
 
        DataGridView2.AllowUserToAddRows = False
        DataGridView2.Rows.Clear()
        workbook = app.Workbooks.Open(filepath)
        worksheet = workbook.Worksheets("ALL")
 
        For i = 2 To worksheet.Rows.Count - 1
            If worksheet.Cells(i, 7).Value = "" Then
                Exit For
            Else
                timer = Trim(worksheet.Cells(i, 7).Value)
                test = worksheet.Cells(i, 14).Value
                spotname = Trim(test)
                typeid = worksheet.Cells(i, 8).Value
                test = worksheet.Cells(i, 11).Value
                index = Trim(test)
                thickness = worksheet.Cells(i, 98).Value
 
                DataGridView2.Rows.Add(timer, spotname, typeid, index, thickness)
            End If
        Next
        workbook.Close()
        app.Quit()
    End Function
 
    Private Sub Compare_Click(sender As Object, e As EventArgs) Handles Compare.Click
        Dim test As String
        Dim encontrado, spotname, robotname, indexnumber, type_ID, thickness As Boolean
        Dim i, m As Integer
        encontrado = False
        spotname = False
        robotname = True
        indexnumber = True
        type_ID = True
        thickness = True
 
        DataGridView3.Rows.Clear()
        DataGridView3.AllowUserToAddRows = False
 
        For i = 0 To DataGridView1.Rows.Count - 1
            For m = 0 To DataGridView2.Rows.Count - 1
                test = Mid(DataGridView2.Rows(m).Cells(1).Value, 1, 10)
                If DataGridView1.Rows(i).Cells(2).Value = DataGridView2.Rows(m).Cells(2).Value And DataGridView1.Rows(i).Cells(0).Value = DataGridView2.Rows(m).Cells(0).Value Then
                    If DataGridView1.Rows(i).Cells(3).Value = DataGridView2.Rows(m).Cells(3).Value And DataGridView1.Rows(i).Cells(4).Value = DataGridView2.Rows(m).Cells(4).Value And DataGridView1.Rows(i).Cells(1).Value = test Then
                        DataGridView3.Rows.Add(DataGridView1.Rows(i).Cells(0).Value, DataGridView1.Rows(i).Cells(1).Value, DataGridView1.Rows(i).Cells(2).Value, DataGridView1.Rows(i).Cells(3).Value, DataGridView1.Rows(i).Cells(4).Value, "OK")
                        encontrado = True
                        spotname = True
                    End If
 
                    If DataGridView1.Rows(i).Cells(1).Value = test And encontrado = False Then
                        spotname = True
                        If DataGridView1.Rows(i).Cells(0).Value <> DataGridView2.Rows(m).Cells(0).Value Then
                            robotname = False
                        End If
                        If DataGridView1.Rows(i).Cells(2).Value <> DataGridView2.Rows(m).Cells(2).Value Then
                            type_ID = False
                        End If
                        If DataGridView1.Rows(i).Cells(4).Value <> DataGridView2.Rows(m).Cells(4).Value Then
                            thickness = False
                        End If
                        If DataGridView1.Rows(i).Cells(1).Value <> DataGridView2.Rows(m).Cells(1).Value Then
                            indexnumber = False
                        End If
                    End If
                End If
            Next
            If encontrado = True Then
                robotname = True
                indexnumber = True
                type_ID = True
            End If
 
            If encontrado = False Then
                If thickness = False Then
                    DataGridView3.Rows.Add(DataGridView1.Rows(i).Cells(0).Value, DataGridView1.Rows(i).Cells(1).Value, DataGridView1.Rows(i).Cells(2).Value, DataGridView1.Rows(i).Cells(3).Value, DataGridView1.Rows(i).Cells(4).Value, "NOK, thickness")
                End If
                If spotname = False Then
                    DataGridView3.Rows.Add(DataGridView1.Rows(i).Cells(0).Value, DataGridView1.Rows(i).Cells(1).Value, DataGridView1.Rows(i).Cells(2).Value, DataGridView1.Rows(i).Cells(3).Value, DataGridView1.Rows(i).Cells(4).Value, "NOK, spotname")
                End If
                If indexnumber = False Then
                    DataGridView3.Rows.Add(DataGridView1.Rows(i).Cells(0).Value, DataGridView1.Rows(i).Cells(1).Value, DataGridView1.Rows(i).Cells(2).Value, DataGridView1.Rows(i).Cells(3).Value, DataGridView1.Rows(i).Cells(4).Value, "NOK, index")
                End If
                If robotname = False Then
                    DataGridView3.Rows.Add(DataGridView1.Rows(i).Cells(0).Value, DataGridView1.Rows(i).Cells(1).Value, DataGridView1.Rows(i).Cells(2).Value, DataGridView1.Rows(i).Cells(3).Value, DataGridView1.Rows(i).Cells(4).Value, "NOK, Robot name")
                End If
            End If
            encontrado = False
            spotname = False
        Next
        For i = 0 To DataGridView3.Rows.Count - 1
            test = DataGridView3.Rows(i).Cells(5).Value.ToString
            If test.Contains("NOK") Then
                DataGridView3.Rows(i).DefaultCellStyle.BackColor = Color.IndianRed
            Else
                DataGridView3.Rows(i).DefaultCellStyle.BackColor = Color.LightGreen
            End If
        Next
    End Sub
 
End Class

Muchas gracias
Nico
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder