Quantcast
Channel: Visual Basic Tips and Tricks
Viewing all articles
Browse latest Browse all 2212

processo Excel non si chiude

$
0
0

Ciao a tutti e buona ripresa,

 sono a richiedere il vostro aiuto perchè sono fermo su una Sub da 2/3 giorni e pur avendo letto e riletto tutto quello che in questo sito avete scritto sull'argomento non riesco proprio ad uscirne. il fatto è che il problema si presenta solo su una Sub mentre su tutte le altre Sub del progetto che fanno più o meno la stessa cosa questo non succede.

Ho realizzato un'applicazione che fra le altre cose estra dei dati da un archivio access e li riversa in un file excel. Per evitare di dover formattare via codice il file excel ho realizzato dei modelli in una cartella specifica. per cui quando vado ad aprire il file ottengo una copia del modello sul quale scarico i dati dell'archivio.

alla fine dell'operazione il file excel viene creato a disposizione dell'utente. Ovviamente il processo excel è presente nell'elenco del Task manager.

La desiderata è che chiudendo il file excel il processo si chiuda.

Questo avviene su tutte le altre Sub tranne che su quella che vi mostro nel seguito.

So che ho commesso un errore ma non riesco a capire quale.

Chiedo cortesemente il vostro aiuto per uscirne.

Grazie,

ciao

PS: l'ambiente in cui sto operando è di Debug.

Dim riga_inc As Integer = 25
                Dim riga_man As Integer = 71
                Dim riga_S_terzi As Integer = 92
                Dim i, j, z, t As Integer

                Dim qry_intestazione As String = "SELECT * FROM intestazione WHERE id_RIG="& id_RIG
                Dim qry_utente As String = "SELECT * FROM utente WHERE id_rig="& id_RIG
                Dim Qry_inconveniente As String = "SELECT * FROM inconveniente WHERE id_rig="& id_RIG
                Dim X As Excel.Application = Nothing
                Dim wb As Excel.Workbook
                Dim ws1, ws2 As Excel.Worksheet

                Dim filetop As String = "E:\progetto_C\traduzione_VbNet\Progetto_C_in_VbNet\Progetto_Garanzia\Resources\template\Copia_temporanea_RIG.xltm"

                Dim stringa_di_connessione As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= "& mio_archivio
                Dim conn As OleDbConnection
                conn = New OleDbConnection(stringa_di_connessione)
                X = New Excel.Application
                wb = X.Workbooks.Open(filetop)


                Using conn
                    conn.Open()
                    Dim command_intestazione As OleDbCommand = New OleDbCommand(qry_intestazione, conn)


                    Dim reader_intestazione As OleDbDataReader = command_intestazione.ExecuteReader()

                    If reader_intestazione.HasRows Then
                        Do While reader_intestazione.Read()

                            '****************************** I N T E S T A Z I O N E ***********************
                            ws1 = CType(wb.Worksheets("Sintesi Intervento"), Excel.Worksheet)
                            ws1.Range("S2").Value = reader_intestazione.Item("Richiesta_intervento_in_garanzia")
                            ws1.Range("S3").Value = reader_intestazione.Item("richiesta_gruppo")
                            ws1.Range("S4").Value = reader_intestazione.Item("richiesta_materiale")
                            ws1.Range("S5").Value = reader_intestazione.Item("richiesta_esubero_manodopera")
                            ws1.Range("AJ3").Value = reader_intestazione.Item("data_compilazione")
                            ws1.Range("AJ5").Value = reader_intestazione.Item("data_apertura_commessa")
                            ws1.Range("AJ7").Value = reader_intestazione.Item("rif_commessa")
                            ws1.Range("I7").Value = reader_intestazione.Item("concessionaria")
                            ws1.Range("I9").Value = reader_intestazione.Item("officina_autorizzata")
                            ws1.Range("F11").Value = reader_intestazione.Item("compilatore")
                            ws1.Range("V11").Value = reader_intestazione.Item("telefono")
                            ws1.Range("AD11").Value = reader_intestazione.Item("email")
                            ws1.Range("F16").Value = reader_intestazione.Item("modello")
                            ws1.Range("X16").Value = reader_intestazione.Item("van")
                            ws1.Range("AL16").Value = reader_intestazione.Item("km")
                            ws1.Range("F18").Value = reader_intestazione.Item("telaio")
                            ws1.Range("X18").Value = reader_intestazione.Item("data_immatricolazione")
                            ws1.Range("AM18").Value = reader_intestazione.Item("veicolo_sotto_contratto_SI")
                            ws1.Range("AP18").Value = reader_intestazione.Item("veicolo_sotto_contratto_NO")
                            ws1.Range("I20").Value = reader_intestazione.Item("cliente")
                            ws1.Range("AL20").Value = reader_intestazione.Item("numero_contratto")
                            ws1.Range("I22").Value = reader_intestazione.Item("ente_venditore")
                            ws1.Range("G27").Value = reader_intestazione.Item("da_inviare_a")
                            ws1.Range("V27").Value = reader_intestazione.Item("indirizzo")
                            ws1.Range("AE25").Value = reader_intestazione.Item("attenzione")
                            '****************************** I N T E S T A Z I O N E ***********************

                            '****************************** U S E R ***************************************
                            Dim command_utente As OleDbCommand = New OleDbCommand(qry_utente, conn)
                            Dim reader_utente As OleDbDataReader = command_utente.ExecuteReader()

                            If reader_utente.HasRows Then
                                reader_utente.Read()
                                ws1.Range("AK99").Value = reader_utente.Item("User")
                                ws1.Range("AK103").Value = reader_utente.Item("Data")
                            End If
                            '****************************** U S E R ***************************************

                            '****************************** I N C O N V E N I E N T E *********************
                            Dim command_inconveniente As OleDbCommand = New OleDbCommand(Qry_inconveniente, conn)
                            Dim reader_inconveniente As OleDbDataReader = command_inconveniente.ExecuteReader()
                            If reader_inconveniente.HasRows Then
                                i = 1
                                Do While reader_inconveniente.Read()

                                    ws2 = CType(wb.Worksheets("Dettaglio Inconveniente "& i), Excel.Worksheet)
                                    ws2.Range("B14").Value = reader_inconveniente.Item("tipo_garanzia")
                                    ws2.Range("F14").Value = reader_inconveniente.Item("cod_inconveniente")
                                    ws2.Range("K14").Value = reader_inconveniente.Item("avaria")
                                    ws2.Range("B17").Value = reader_inconveniente.Item("descrizione_anomalia")
                                    ws2.Range("AJ14").Value = reader_inconveniente.Item("Intero_inconveniente_non_riconosciuto")
                                    ws2.Range("O17").Value = reader_inconveniente.Item("Note")

                                    '**** P A R T  N U M B E R *********************
                                    Dim id_inconveniente As Long = CLng(reader_inconveniente.Item("Id_inconveniente"))
                                    Dim qry_pn As String = "SELECT * FROM partnumber WHERE id_inconveniente="& id_inconveniente
                                    Dim command_partnumber As OleDbCommand = New OleDbCommand(qry_pn, conn)
                                    Dim reader_partnumber As OleDbDataReader = command_partnumber.ExecuteReader()
                                    j = 0
                                    If reader_partnumber.HasRows Then
                                        Do While reader_partnumber.Read()

                                            ws2.Range("B"& riga_inc + j).Value = reader_partnumber.Item("part_number")
                                            ws2.Range("C"& riga_inc + j).Value = reader_partnumber.Item("descrizione")
                                            ws2.Range("D"& riga_inc + j).Value = reader_partnumber.Item("tipo")
                                            ws2.Range("E"& riga_inc + j).Value = reader_partnumber.Item("matricola")
                                            ws2.Range("P"& riga_inc + j).Value = reader_partnumber.Item("pn_riconosciuto_si")
                                            ws2.Range("R"& riga_inc + j).Value = reader_partnumber.Item("pn_riconosciuto_no")
                                            ws2.Range("U"& riga_inc + j).Value = reader_partnumber.Item("invio_in_conto_garanzia")
                                            ws2.Range("X"& riga_inc + j).Value = reader_partnumber.Item("pn_vendita_con_sconto_SI")
                                            ws2.Range("AA"& riga_inc + j).Value = reader_partnumber.Item("forfettario_SI")
                                            ws2.Range("F"& riga_inc + j).Value = reader_partnumber.Item("quantita")
                                            ws2.Range("G"& riga_inc + j).Value = reader_partnumber.Item("importo_unitario_a_listino")
                                            ws2.Range("I"& riga_inc + j).Value = reader_partnumber.Item("totale_richiesto")
                                            ws2.Range("Y"& riga_inc + j).Value = reader_partnumber.Item("pn_vendita_con_sconto_perc")
                                            ws2.Range("AB"& riga_inc + j).Value = reader_partnumber.Item("forfettario_quota_Iveco")
                                            ws2.Range("AG"& riga_inc + j).Value = reader_partnumber.Item("totale_pn")

                                            j = j + 1
                                        Loop
                                    End If
                                    '**** P A R T  N U M B E R *********************
                                    '**** M A N O D O P E R A **********************
                                    Dim qry_MdO As String = "SELECT * FROM Manodopera WHERE id_inconveniente="& id_inconveniente
                                    Dim command_MdO As OleDbCommand = New OleDbCommand(qry_MdO, conn)
                                    Dim reader_MdO As OleDbDataReader = command_MdO.ExecuteReader()
                                    z = 0
                                    If reader_MdO.HasRows Then
                                        Do While reader_MdO.Read()
                                            ws2.Range("B"& riga_man + z).Value = reader_MdO.Item("codice_operazione")
                                            ws2.Range("C"& riga_man + z).Value = reader_MdO.Item("U_L")
                                            ws2.Range("D"& riga_man + z).Value = reader_MdO.Item("descrizione")
                                            ws2.Range("G71").Value = reader_MdO.Item("importo_orario")
                                            ws2.Range("I"& riga_man + z).Value = reader_MdO.Item("totale_richiesto_MdO")
                                            ws2.Range("P"& riga_man + z).Value = reader_MdO.Item("MdO_riconosciuto_SI")
                                            ws2.Range("R"& riga_man + z).Value = reader_MdO.Item("MdO_riconosciuto_NO")
                                            ws2.Range("AA"& riga_man + z).Value = reader_MdO.Item("MdO_forfettario_SI")
                                            ws2.Range("AB"& riga_man + z).Value = reader_MdO.Item("MdO_forfettario_Quota_Iveco")
                                            ws2.Range("AG"& riga_man + z).Value = reader_MdO.Item("totale_MdO")

                                            z = z + 1
                                        Loop
                                    End If
                                    '**** M A N O D O P E R A **********************
                                    '**** S P E S E  T E R Z I **********************
                                    Dim qry_speseterzi As String = "SELECT * FROM Spese_terzi WHERE id_inconveniente="& id_inconveniente
                                    Dim command_speseterzi As OleDbCommand = New OleDbCommand(qry_speseterzi, conn)
                                    Dim reader_speseterzi As OleDbDataReader = command_speseterzi.ExecuteReader()
                                    t = 0
                                    If reader_speseterzi.HasRows Then
                                        Do While reader_speseterzi.Read()
                                            ws2.Range("B"& riga_S_terzi + t).Value = reader_speseterzi.Item("descrizione_lavorazione")
                                            ws2.Range("I"& riga_S_terzi + t).Value = reader_speseterzi.Item("importo_richiesto")
                                            ws2.Range("P"& riga_S_terzi + t).Value = reader_speseterzi.Item("LT_riconosciuto_SI")
                                            ws2.Range("R"& riga_S_terzi + t).Value = reader_speseterzi.Item("LT_riconosciuto_NO")
                                            ws2.Range("AA"& riga_S_terzi + t).Value = reader_speseterzi.Item("LT_Forfettario_SI")
                                            ws2.Range("AB"& riga_S_terzi + t).Value = reader_speseterzi.Item("LT_forfettario_quota_Iveco")
                                            ws2.Range("AG"& riga_S_terzi + t).Value = reader_speseterzi.Item("LT_Totale")
                                            t = t + 1
                                        Loop
                                    End If

                                    '**** S P E S E  T E R Z I **********************
                                    i = i + 1
                                    Marshal.FinalReleaseComObject(ws2)
                                    ws2 = Nothing
                                Loop
                                Marshal.FinalReleaseComObject(ws1)
                                ws1 = Nothing
                            End If

                            '****************************** I N C O N V E N I E N T E *********************
                        Loop

                        X.Visible = True


                        Marshal.FinalReleaseComObject(wb)
                        wb = Nothing
                        Marshal.FinalReleaseComObject(X)
                        X = Nothing

                        GC.Collect()
                        GC.WaitForPendingFinalizers()
                        GC.Collect()
                    End If

                End Using

                conn.Close()
 


Viewing all articles
Browse latest Browse all 2212