Visualizzazione dei risultati da 1 a 7 su 7
  1. #1
    Utente di HTML.it
    Registrato dal
    Dec 2016
    Messaggi
    7

    [VB.NET] Processo di Excel resta aperto nel taskmanager

    Buongiorno a tutti,
    scusatemi ma so che più volte è stato affrontato questo argomento, ma ogni soluzione seguita non mi ha risolto l'anomalia.

    Non riesco a chiudere il processo di Excel con VB.net 2008

    codice:
    Imports System.Threading
    
    Public Class Form1 : Inherits Form
    
        Dim Trasferisci As Threading.Thread
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            Dim Trasferisci As New Threading.Thread(AddressOf trasferisciSSC)
               Trasferisci.Start()
            End Sub
    
        Private Sub trasferisciSSC()
    
            Dim oExcel As New Object
            oExcel = CreateObject("Excel.Application")
            Dim oBook As Object
            oBook = oExcel.Workbooks.Open("...\Test Kill Excel.xlsx")
            Dim osheet0 As Object
            osheet0 = oBook.Worksheets(1)
    
            osheet0.Activate()
            osheet0.Cells(1, 1).value = TextBox1.Text
    
            oBook.Close(SaveChanges:=True)
            osheet0 = Nothing
            oBook = Nothing
            oExcel = Nothing
    
        End Sub
    
    End Class

    Provando ad escludere i le varie operazioni sono arrivato a questo:

    codice:
    Imports System.Threading
    
    Public Class Form1 : Inherits Form
    
        Dim Trasferisci As Threading.Thread
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim Trasferisci As New Threading.Thread(AddressOf trasferisciSSC)
            Trasferisci.Start()
            End Sub
    
        Private Sub trasferisciSSC()
    
            Dim oExcel As New Object
            oExcel = CreateObject("Excel.Application")
    
            oExcel = Nothing
    
        End Sub
    
    End Class
    Anche solo creando un nuovo processo , già non riesce a chiuderlo...

    Cosa non distruggo correttamente?

    Fino ad oggi ho utilizzato un KILL Excell, ma come ben sapete mi "killa" tutti processi presenti.

  2. #2
    Utente di HTML.it L'avatar di oregon
    Registrato dal
    Jul 2005
    residenza
    Roma
    Messaggi
    36,480
    Prova ad aggiungere come ultime righe

    GC.Collect()
    GC.WaitForPendingFinalizers()
    No MP tecnici (non rispondo nemmeno!), usa il forum.

  3. #3
    Utente di HTML.it
    Registrato dal
    Dec 2016
    Messaggi
    7
    Quote Originariamente inviata da oregon Visualizza il messaggio
    Prova ad aggiungere come ultime righe

    GC.Collect()
    GC.WaitForPendingFinalizers()

    OK Grazie Perfetto per questo semplice caso.
    Ora però nello specifico:

    codice:
    Imports System.Threading
    
    Friend Class frmArrivo : Inherits Form
    
    Dim Aggiorna As Threading.Thread
        Private Sub Command2_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Command2.Click
    
            Dim Aggiorna As New Threading.Thread(AddressOf AggiornaFile)
            Aggiorna.Start()
            End Sub
    
        'Aggiungi
    
        Private Sub AggiornaFile()
    
            MessageViewer1.AddMessage(1, "Aggiornamento in corso...")
            Dim oExcel As Object
            oExcel = CreateObject("Excel.Application")
            Dim oBook As Object
            oBook = oExcel.Workbooks.Open("...\Gestione materiale.xlsx")
            Dim oSheet As Object
            oSheet = oBook.Worksheets(4)
            Dim i As Object
         
            oSheet.Activate()
    
            ProgressBar1.Value = 0
    
            For i = 3 To 10000
                ProgressBar1.Value = ProgressBar1.Value + 1
    
                If Trim(oSheet.Cells(i, 1).value) = "" Then
    
                    oSheet.Cells(i, 1).value = TextBox1.Text       'Terra
                    oSheet.Cells(i, 2).value = Text1.Text          'P/N
                    oSheet.Cells(i, 3).value = Combo1.Text         'Tipo
                    oSheet.Cells(i, 4).value = Text2.Text          'S/N
                    oSheet.Cells(i, 5).value = Text11.Text         'Provenienza
                    oSheet.Cells(i, 6).value = Text5.Text          'DDT RFI
                    oSheet.Cells(i, 7).value = CDate(Text4.Text)   'Data DDT RFI
                    oSheet.Cells(i, 8).value = Combo8.Text          'Esito
                    oSheet.Cells(i, 9).value = Combo8.Text          'Magazzino Mermec
    
                    i = 10001
    
                End If
            Next i
    
    
            oBook.Close(SaveChanges:=True)
            oSheet = Nothing
            oBook = Nothing
            oExcel = Nothing
            i = Nothing
    
            GC.Collect()
            GC.WaitForPendingFinalizers()
    
            ProgressBar1.Value = ProgressBar1.Maximum
            MessageViewer1.AddMessage(1, "Scrittura in file " & Combo1.Text & " di S/N " & Text2.Text & " effettuata con successo")
            
           
    
        End Sub
    End Class
    Resta aperto...isolando il caso FOR / NEXT funziona (Situazione precedente)
    Ora vi chiedo cosa devo e come devo distruggere?

    Grazie

  4. #4
    Utente di HTML.it L'avatar di gibra
    Registrato dal
    Apr 2008
    residenza
    Italy
    Messaggi
    4,244
    Prova a guardare il mio progetto su Excel

    VS2010 - Early-Late Binding in VB.NET con Excel
    http://nuke.vbcorner.net/Projects/NE...S/Default.aspx

  5. #5
    Utente di HTML.it
    Registrato dal
    Dec 2016
    Messaggi
    7
    Quote Originariamente inviata da gibra Visualizza il messaggio
    Prova a guardare il mio progetto su Excel

    VS2010 - Early-Late Binding in VB.NET con Excel
    http://nuke.vbcorner.net/Projects/NE...S/Default.aspx
    Ciao gibra,
    ho scaricato il tuo file, per� ho una marea di errori solo aprendolo:

    codice:
    Avviso    19    Impossibile trovare il componente "Microsoft.Office.Interop.Excel" a cui si � fatto riferimento.     
    Errore    4    Tipo 'Microsoft.Office.Interop.Excel.Application' non definito.    C:\Documents and Settings\giovanni.leuci\Desktop\Progetto\VB test\Test kill excell\Test kill excell\Test kill excell\EarlyLateBinding_Excel\Form1.vb    6    21    EarlyLateBinding_Excel
    Errore    5    Tipo 'Microsoft.Office.Interop.Excel.Workbook' non definito.    C:\Documents and Settings\giovanni.leuci\Desktop\Progetto\VB test\Test kill excell\Test kill excell\Test kill excell\EarlyLateBinding_Excel\Form1.vb    7    26    EarlyLateBinding_Excel
    Errore    6    Tipo 'Microsoft.Office.Interop.Excel.Worksheet' non definito.    C:\Documents and Settings\giovanni.leuci\Desktop\Progetto\VB test\Test kill excell\Test kill excell\Test kill excell\EarlyLateBinding_Excel\Form1.vb    8    27    EarlyLateBinding_Excel
    Errore    7    Tipo 'Microsoft.Office.Interop.Excel.Range' non definito.    C:\Documents and Settings\giovanni.leuci\Desktop\Progetto\VB test\Test kill excell\Test kill excell\Test kill excell\EarlyLateBinding_Excel\Form1.vb    9    23    EarlyLateBinding_Excel
    Errore    12    'GeneratedCodeAttribute' � ambiguo nello spazio dei nomi 'System.CodeDom.Compiler'.    C:\Documents and Settings\giovanni.leuci\Desktop\Progetto\VB test\Test kill excell\Test kill excell\Test kill excell\EarlyLateBinding_Excel\My Project\Settings.Designer.vb    18    6    EarlyLateBinding_Excel
    Errore    1    'IContainer' � ambiguo nello spazio dei nomi 'System.ComponentModel'.    C:\Documents and Settings\giovanni.leuci\Desktop\Progetto\VB test\Test kill excell\Test kill excell\Test kill excell\EarlyLateBinding_Excel\Form1.Designer.vb    18    27    EarlyLateBinding_Excel
    Errore    13    'EditorBrowsableAttribute' � ambiguo nello spazio dei nomi 'System.ComponentModel'.    C:\Documents and Settings\giovanni.leuci\Desktop\Progetto\VB test\Test kill excell\Test kill excell\Test kill excell\EarlyLateBinding_Excel\My Project\Settings.Designer.vb    19    6    EarlyLateBinding_Excel
    Errore    14    'ApplicationSettingsBase' � ambiguo nello spazio dei nomi 'System.Configuration'.    C:\Documents and Settings\giovanni.leuci\Desktop\Progetto\VB test\Test kill excell\Test kill excell\Test kill excell\EarlyLateBinding_Excel\My Project\Settings.Designer.vb    21    18    EarlyLateBinding_Excel
    Errore    15    'ApplicationSettingsBase' � ambiguo nello spazio dei nomi 'System.Configuration'.    C:\Documents and Settings\giovanni.leuci\Desktop\Progetto\VB test\Test kill excell\Test kill excell\Test kill excell\EarlyLateBinding_Excel\My Project\Settings.Designer.vb    23    62    EarlyLateBinding_Excel
    Errore    9    'GeneratedCodeAttribute' � ambiguo nello spazio dei nomi 'System.CodeDom.Compiler'.    C:\Documents and Settings\giovanni.leuci\Desktop\Progetto\VB test\Test kill excell\Test kill excell\Test kill excell\EarlyLateBinding_Excel\My Project\Resources.Designer.vb    24    6    EarlyLateBinding_Excel
    Errore    16    'EditorBrowsableAttribute' � ambiguo nello spazio dei nomi 'System.ComponentModel'.    C:\Documents and Settings\giovanni.leuci\Desktop\Progetto\VB test\Test kill excell\Test kill excell\Test kill excell\EarlyLateBinding_Excel\My Project\Settings.Designer.vb    31    68    EarlyLateBinding_Excel
    Errore    2    'ISupportInitialize' � ambiguo nello spazio dei nomi 'System.ComponentModel'.    C:\Documents and Settings\giovanni.leuci\Desktop\Progetto\VB test\Test kill excell\Test kill excell\Test kill excell\EarlyLateBinding_Excel\Form1.Designer.vb    31    28    EarlyLateBinding_Excel
    Errore    17    'Save' non � un membro di 'Excel_test.My.MySettings'.    C:\Documents and Settings\giovanni.leuci\Desktop\Progetto\VB test\Test kill excell\Test kill excell\Test kill excell\EarlyLateBinding_Excel\My Project\Settings.Designer.vb    34    17    EarlyLateBinding_Excel
    Errore    10    'EditorBrowsableAttribute' � ambiguo nello spazio dei nomi 'System.ComponentModel'.    C:\Documents and Settings\giovanni.leuci\Desktop\Progetto\VB test\Test kill excell\Test kill excell\Test kill excell\EarlyLateBinding_Excel\My Project\Resources.Designer.vb    37    10    EarlyLateBinding_Excel
    Errore    8    Tipo 'Microsoft.Office.Interop.Excel.Application' non definito.    C:\Documents and Settings\giovanni.leuci\Desktop\Progetto\VB test\Test kill excell\Test kill excell\Test kill excell\EarlyLateBinding_Excel\Form1.vb    37    32    EarlyLateBinding_Excel
    Errore    11    'EditorBrowsableAttribute' � ambiguo nello spazio dei nomi 'System.ComponentModel'.    C:\Documents and Settings\giovanni.leuci\Desktop\Progetto\VB test\Test kill excell\Test kill excell\Test kill excell\EarlyLateBinding_Excel\My Project\Resources.Designer.vb    52    10    EarlyLateBinding_Excel
    Errore    18    'HelpKeywordAttribute' � ambiguo nello spazio dei nomi 'System.ComponentModel.Design'.    C:\Documents and Settings\giovanni.leuci\Desktop\Progetto\VB test\Test kill excell\Test kill excell\Test kill excell\EarlyLateBinding_Excel\My Project\Settings.Designer.vb    66    10    EarlyLateBinding_Excel
    Errore    3    'ISupportInitialize' � ambiguo nello spazio dei nomi 'System.ComponentModel'.    C:\Documents and Settings\giovanni.leuci\Desktop\Progetto\VB test\Test kill excell\Test kill excell\Test kill excell\EarlyLateBinding_Excel\Form1.Designer.vb    105    28    EarlyLateBinding_Excel
    Ho impostato le propriet� come descritto ma vedo questa miriade di errori.
    Controllando nei riferimenti , mi dice che � imoossibile trovare :
    Microsoft.Office.Interop.Excel
    Vi ricordo che utilizzo VB.net 2008 non vorrei sia questo il motivo.


    Comunque, tornando al mio applicativo, aggiungendo le 2 righe suggerite da oregon al termine delle operazioni accade questo:


    • Utilizzando la prima volta il programma si apre un processo di EXCEL e non si richiude
    • Utilizzandolo le volte successive, invece, se ne apre un'altra e si richiude, lasciando aperti eventuali altri file EXCEL (cosa corretta)
    • Chiudendo l'applicativo mi si chiude anche il processo aperto la prima volta.


    E' un metodo un po' grossolano, ma gi� il fatto che non sono costretto a killare EXCEL � un passone in avanti.

    Se avete altri accorgimenti Vi prego di aggiornarmi.

    Per il momento vi ringrazio

  6. #6
    Utente di HTML.it L'avatar di gibra
    Registrato dal
    Apr 2008
    residenza
    Italy
    Messaggi
    4,244
    Non c'entra nulla, il tuo problema è Microsoft.Office.Interop.Excel.
    Verifica questo...

    Certo è che usare questi percorsi così lunghi non è proprio il massimo...
    "C:\Documents and Settings\giovanni.leuci\Desktop\Progetto\VB test\Test kill excell\Test kill excell\Test kill excell\EarlyLateBinding_Excel\My Project\Resources.Designer.vb"

  7. #7
    Utente di HTML.it
    Registrato dal
    Dec 2016
    Messaggi
    7
    Quote Originariamente inviata da gibra Visualizza il messaggio

    Certo è che usare questi percorsi così lunghi non è proprio il massimo...
    "C:\Documents and Settings\giovanni.leuci\Desktop\Progetto\VB test\Test kill excell\Test kill excell\Test kill excell\EarlyLateBinding_Excel\My Project\Resources.Designer.vb"

    Si hai ragione

Tag per questa discussione

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.