app not running when changing the combobox value
Hello guys, i asked this question again but didn't seem to find an answer can you please healp me again.
im trying to run the application each time with a different comobox value (each time wit ha different worksheet).
but the problem that the code works for the first time i click on the button 2 and when i try to change the combobox value run it with a different comobox value it shows this erreur "Object reference not set to an instance of an object" on the ws1.
can you please show me how to run the app multiple times without this problem.
Code:
Imports Microsoft.Office.Interop.Excel
Imports Microsoft.Office.Interop
Imports System.Windows.Forms.OpenFileDialog
Dim xlWorkSheet As Microsoft.Office.Interop.Excel.Worksheet
Dim xlApp As New Microsoft.Office.Interop.Excel.Application
Dim work1 As Excel.Workbook
Dim ws1 As Excel.Worksheet
Dim cell As Excel.Range
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
End Sub
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
OpenFileDialog1.ShowDialog()
TextBox1.Text = OpenFileDialog1.FileName
For Each xlWorkSheet In xlApp.Sheets
ComboBox1.Items.Add(xlWorkSheet.Name)
Next
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
work1 = xlApp.Workbooks.Open(TextBox1.Text)
ws1 = xlApp.Workbooks.Open(TextBox1.Text).Worksheets(ComboBox1.SelectedItem) ' here where the error shows
cell=ws1.Cells(3, 5)
cell.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red)
work1.Close()
xlApp.Quit()
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp) : xlApp = Nothing
System.Runtime.InteropServices.Marshal.ReleaseComObject(work1) : work1 = Nothing
System.Runtime.InteropServices.Marshal.ReleaseComObject(ws1) : ws1 = Nothing
end sub
Re: app not running when changing the combobox value
That's not the way it works. Stick with the thread you already have open.