Link to home
Start Free TrialLog in
Avatar of Wayne Taylor (webtubbs)
Wayne Taylor (webtubbs)Flag for Australia

asked on

Killing Excel instance

Hi Guys,

I'm having trouble trying to kill an instance of excel from my app. This is the applicable form code....

Imports Microsoft.Office.Interop.Excel.XlDirection
Imports Microsoft.Office.Interop.Excel.Constants
Imports Microsoft.Office.Interop.Excel.XlLookAt
Imports Microsoft.Office.Interop.Excel.ApplicationClass
Imports Microsoft.Office.Interop.Excel.XlFindLookIn
--------------------------------------------------------------------------------------------------------------------
Public Class frmElement
    Inherits System.Windows.Forms.Form
--------------------------------------------------------------------------------------------------------------------
#Region " Windows Form Designer generated code "
 (blah, blah, blah....)
#End Region
--------------------------------------------------------------------------------------------------------------------
    Dim xlApp As Object, ws As Object, wb As Object, fnd As Object
--------------------------------------------------------------------------------------------------------------------
    Private Sub ComboBox10_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox10.SelectedIndexChanged

        Me.ComboBox11.Items.Clear()
        Me.ComboBox12.Items.Clear()
        Me.ComboBox13.Items.Clear()
        Me.ComboBox14.Items.Clear()
        Me.ComboBox15.Items.Clear()
        Me.ComboBox16.Items.Clear()
        Me.ComboBox17.Items.Clear()
        Me.ComboBox18.Items.Clear()

        fnd = ws.range("1:1").find(Replace(Me.ComboBox10.SelectedItem, "D_", ""), , xlValues, xlWhole)
        If Not fnd Is Nothing Then
            Dim r As Long = 2
            Do While ws.cells(r, fnd.column).value <> ""
                Me.ComboBox12.Items.Add(ws.cells(r, fnd.column).value)
                Me.ComboBox13.Items.Add(ws.cells(r, fnd.column).value)
                Me.ComboBox14.Items.Add(ws.cells(r, fnd.column).value)
                Me.ComboBox15.Items.Add(ws.cells(r, fnd.column).value)
                Me.ComboBox16.Items.Add(ws.cells(r, fnd.column).value)
                Me.ComboBox17.Items.Add(ws.cells(r, fnd.column).value)
                Me.ComboBox18.Items.Add(ws.cells(r, fnd.column).value)
                Me.ComboBox11.Items.Add(ws.cells(r, fnd.column).value)
                r = r + 1
            Loop
        Else
            MsgBox("Method could not be found. There are a number of possible causes for this...." & vbCrLf & _
                    vbCrLf & "1) A Method name was manually typed into the Dropdown box." & vbCrLf & _
                    "   Do not do this. Select a Method from the dropdown list instead." & vbCrLf & vbCrLf & _
                    "2) A method was deleted after creation of this batch." & vbCrLf & _
                    "   Re-select a method or re-create the missing method.")
        End If
        fnd = Nothing
    End Sub
------------------------------------------------------------------------------------------------------------------------------
    Private Sub frmElement_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        xlApp = CreateObject("Excel.Application")
        xlApp.visible = False
        wb = xlApp.workbooks.open(AppPath & "Methods.xls")
        ws = wb.worksheets("Sheet1")
    End Sub
-----------------------------------------------------------------------------------------------------------------------------
    Private Sub btnDone_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDone.Click
        ws = Nothing
        wb.close(False)
        wb = Nothing
        xlApp.quit()
        xlApp = Nothing
        Me.Close()
    End Sub
---------------------------------------------------------------------------------------------------------------------------
    Private Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.btnDone_Click(Nothing, Nothing)
    End Sub
---------------------------------------------------------------------------------------------------------------------------

But it doesn't seem to want to Quit the Excel application. Any ideas?

Cheers,

Wayne
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Avatar of Wayne Taylor (webtubbs)

ASKER

angelIII,

The accepted answer in that thread is basically the same as my code above.

I should say that if I have a code like this....

        Dim xlApp As Object, ws As Object, wb As Object
        xlApp = CreateObject("Excel.Application")
        xlApp.visible = False
        wb = xlApp.workbooks.open(AppPath & "Methods.xls")
        ws = wb.worksheets("Sheet1")
        ' do stuff
        ws = Nothing
        wb.close(False)
        wb = Nothing
        xlApp.quit()
        xlApp = Nothing

....it shuts down OK.

Wayne
ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ahh, missed that bit. Cheers :)

Wayne