Results 1 to 19 of 19

Thread: Work with the application each time with a different comoboxvalue

Threaded View

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2021
    Posts
    39

    Unhappy Work with the application each time with a different comoboxvalue

    hello guys , i need your help with my application.

    im trying to run the application each time with a different comobox value. 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 probleme .
    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 xlworkbook 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
            xlworkbook = xlApp.Workbooks.Open(TextBox1.Text)
    
            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 Button3.Click
    
            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)
                xlworkbook.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
    thank you guys for helping in advanced
    Last edited by highfly884; Sep 27th, 2021 at 06:36 AM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width