-2

I have created some code to populate combo boxes on my form from a excel file when the form loads.

As part of the code, it is supposed to release the objects associated but it does not

        Dim excel As New Excel.Application
    Dim w As Excel.Workbook = excel.Workbooks.Open("C:\Email Template\Violations Log\Violations Log.xlsx")
    Dim sheet As Excel.Worksheet = w.Worksheets("Individual Data")
    Dim r As Excel.Range = sheet.Range("A2:A300")
    Dim array(,) As Object = r.Value(excel.XlRangeValueDataType.xlRangeValueDefault)
    Dim sheet2 As Excel.Worksheet = w.Worksheets("Category")
    Dim s As Excel.Range = sheet2.Range("A2:A20")
    Dim array2(,) As Object = s.Value(excel.XlRangeValueDataType.xlRangeValueDefault)
    Dim bound0 As Integer = array.GetUpperBound(0)
    Dim bound1 As Integer = array.GetUpperBound(1)
    Dim j As Integer
    Dim x As Integer
    Dim s1 As String
    If array IsNot Nothing Then


        ' Loop over all elements.
        For j = 1 To bound0
            For x = 1 To bound1
                s1 = array(j, x)
                If s1 IsNot Nothing Then
                    If Not ComboBox1.Items.Contains(s1.ToString) Then
                        ComboBox1.Items.Add(s1.ToString)
                    End If
                End If
            Next
        Next
    End If

    If array IsNot Nothing Then
        ' Loop over all elements.
        For j = 1 To bound0
            For x = 1 To bound1
                s1 = array2(j, x)
                If s1 IsNot Nothing Then
                    If Not ComboBox2.Items.Contains(s1.ToString) Then
                        ComboBox2.Items.Add(s1.ToString)
                    End If
                End If
            Next
        Next
    End If
    w.Close(False)
    excel.Quit()

    ReleaseObject(excel.XlRangeValueDataType.xlRangeValueDefault)
    ReleaseObject(excel)
    ReleaseObject(array)
    ReleaseObject(array2)
    ReleaseObject(r)
    ReleaseObject(s)
    ReleaseObject(sheet)
    ReleaseObject(sheet2)
    ReleaseObject(w)
    ReleaseObject(bound0)
    ReleaseObject(bound1)
    ReleaseObject(j)
    ReleaseObject(x)
    ReleaseObject(s1)

Ive tried to release every object that is referenced but it still has a connection to the excel document.

Have I missed an object? or something bigger?

I have another code that copies from the form to that same excel document and that does not leave any processes open. (I have to kill the excel process that is created from loading the form to be able to use the code to copy data)

Any help would be greatly appreciated.

Hulk Smash 93
  • 55
  • 1
  • 8
  • Possible duplicate of [Application not quitting after calling quit](https://stackoverflow.com/questions/15697282/application-not-quitting-after-calling-quit) – Trevor Oct 31 '18 at 10:41
  • Hi, this is the code I used to release the objects. Its exactly the same. Its weird, because it works for another sub, but not this one. – Hulk Smash 93 Oct 31 '18 at 10:45
  • Possible duplicate of [VB.NET Excel process won't close no matter what](https://stackoverflow.com/questions/48384650/vb-net-excel-process-wont-close-no-matter-what) – Craig Oct 31 '18 at 13:52

1 Answers1

0

Figured it out.

The code would stop running and not complete the release object section of the code. Using debugging I found that I was getting an error message which was muted. I resolved the error message and now it runs fine.

Thanks for trying to help.

Hulk Smash 93
  • 55
  • 1
  • 8