Wayne Taylor (webtubbs)
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.E xcel.XlDir ection
Imports Microsoft.Office.Interop.E xcel.Const ants
Imports Microsoft.Office.Interop.E xcel.XlLoo kAt
Imports Microsoft.Office.Interop.E xcel.Appli cationClas s
Imports Microsoft.Office.Interop.E xcel.XlFin dLookIn
-------------------------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------
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_SelectedIndexCh anged(ByVa l sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox10.SelectedIndexCh anged
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(Repla ce(Me.Comb oBox10.Sel ectedItem, "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.Applic ation")
xlApp.visible = False
wb = xlApp.workbooks.open(AppPa th & "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
I'm having trouble trying to kill an instance of excel from my app. This is the applicable form code....
Imports Microsoft.Office.Interop.E
Imports Microsoft.Office.Interop.E
Imports Microsoft.Office.Interop.E
Imports Microsoft.Office.Interop.E
Imports Microsoft.Office.Interop.E
--------------------------
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_SelectedIndexCh
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(Repla
If Not fnd Is Nothing Then
Dim r As Long = 2
Do While ws.cells(r, fnd.column).value <> ""
Me.ComboBox12.Items.Add(ws
Me.ComboBox13.Items.Add(ws
Me.ComboBox14.Items.Add(ws
Me.ComboBox15.Items.Add(ws
Me.ComboBox16.Items.Add(ws
Me.ComboBox17.Items.Add(ws
Me.ComboBox18.Items.Add(ws
Me.ComboBox11.Items.Add(ws
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.Applic
xlApp.visible = False
wb = xlApp.workbooks.open(AppPa
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
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.Applic ation")
xlApp.visible = False
wb = xlApp.workbooks.open(AppPa th & "Methods.xls")
ws = wb.worksheets("Sheet1")
' do stuff
ws = Nothing
wb.close(False)
wb = Nothing
xlApp.quit()
xlApp = Nothing
....it shuts down OK.
Wayne
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.Applic
xlApp.visible = False
wb = xlApp.workbooks.open(AppPa
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Ahh, missed that bit. Cheers :)
Wayne
Wayne
https://www.experts-exchange.com/questions/21245498/Excel-instance-will-not-release-when-opening-existing-workbook-but-releases-perfectly-when-adding-a-new-workbook.html