-
Apr 19th, 2004, 10:09 AM
#1
Thread Starter
Lively Member
Problems with Excel Interop
Hi,
I'm trying to write a vb.net application that automates Excel (11).
This is fine, but I can't get it to close down after I've finished. I'm using the quit method of the Excel.Application object and setting the object to nothing, but the process is left running in the background.
Can anyone advise.
Many thanks
Nick
-
Apr 19th, 2004, 09:17 PM
#2
Make sure you aren't holding a reference to anything in the excel application. For instance make sure any worksheets or whatever are set to nothing or disposed or at least fall out of scope otherwise the GC will not collect those bits and leave excel open.
-
Apr 20th, 2004, 04:51 AM
#3
Thread Starter
Lively Member
I'm not, I've even tried just creating the application and doing nothing with it, I'm using the Quit method and setting it to nothing. Still leaves the process running
-
Apr 20th, 2004, 09:57 PM
#4
Hmm post your code. I have used Excel Interop without this problem.
-
Nov 8th, 2004, 10:07 AM
#5
Fanatic Member
I know this is an old post, but I have the same problem.
VB Code:
Dim xlApp As New Excel.Application
Dim xlBook As Excel.Workbook
Dim strXLFile As String = "C:\inetpub\wwwroot\mcclure\" & Left(ListBox2.SelectedItem.Text, 11)
xlBook = xlApp.Workbooks.Open(strXLFile)
xlBook.Close(savechanges:=False)
xlBook = Nothing
xlApp.Quit()
xlApp = Nothing
Excel is left running in the background.
Thanks
-
Nov 8th, 2004, 10:11 AM
#6
Hi
Try this before you set your xlapp to nothing
VB Code:
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp)
Regards
Jorge
"The dark side clouds everything. Impossible to see the future is."
-
Nov 8th, 2004, 10:20 AM
#7
Fanatic Member
I gave it a shot and Excel is still not closing down.
VB Code:
Dim xlApp As New Excel.Application
Dim xlBook As Excel.Workbook
Dim strXLFile As String = "C:\inetpub\wwwroot\mcclure\" & Left(ListBox2.SelectedItem.Text, 11)
xlBook = xlApp.Workbooks.Open(strXLFile)
xlBook.Close(savechanges:=False)
xlBook = Nothing
xlApp.Quit()
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp)
xlApp = Nothing
Would it matter that this is in web application?
-
Nov 8th, 2004, 10:38 AM
#8
Originally posted by indydavid32
I gave it a shot and Excel is still not closing down.
VB Code:
Dim xlApp As New Excel.Application
Dim xlBook As Excel.Workbook
Dim strXLFile As String = "C:\inetpub\wwwroot\mcclure\" & Left(ListBox2.SelectedItem.Text, 11)
xlBook = xlApp.Workbooks.Open(strXLFile)
xlBook.Close(savechanges:=False)
xlBook = Nothing
xlApp.Quit()
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp)
xlApp = Nothing
Would it matter that this is in web application?
Probably yes try closing the webpage that uses excel, to see if its closed.
Regards
Jorge
"The dark side clouds everything. Impossible to see the future is."
-
Nov 8th, 2004, 10:44 AM
#9
Fanatic Member
Probably yes try closing the webpage that uses excel, to see if its closed.
I tried that as well. I'm actually doing my testing from the VS programming environment. I also tried closing down VS .Net and it's still there.
-
Nov 8th, 2004, 11:13 AM
#10
I've never an asp.net application that uses excel interop before. Just closing the windows application should terminate excel.
Since its not working you will have to use the process class .kill method.
Regards
Jorge
"The dark side clouds everything. Impossible to see the future is."
-
Nov 8th, 2004, 12:08 PM
#11
Fanatic Member
Hmmm, wish there was another way....
By the way, I couldn't get Process.kill to work. I had to do the following for anyone else reading this post.
VB Code:
Dim proc As Process
For Each proc In Process.GetProcesses
If UCase(proc.ProcessName) = "EXCEL" Then
proc.Kill()
End If
Next
-
Nov 8th, 2004, 12:25 PM
#12
Hi
Try this instead
[vbocde]
Dim localByName As Process() = Process.GetProcessesByName("excel")
For Each process As Process In localByName
process.Kill()
Next
[/vbocde]
Regards
Jorge
"The dark side clouds everything. Impossible to see the future is."
-
Nov 8th, 2004, 01:35 PM
#13
Fanatic Member
That works.
I still wish there was another way. What if the user had an excel spreadsheet up when they run this program? It will be shut down whether they want it shut down or not.
Oh well, I guess I will just have to warn them before they run it.
Thanks for all your help Jorge
-
Nov 8th, 2004, 05:41 PM
#14
Originally posted by indydavid32
That works.
Oh well, I guess I will just have to warn them before they run it.
Thanks for all your help Jorge
Well there's a API function that returns the PID of your excel app on creation and then use that PID to kill it. I don't the code here at home but tomorrow morning i will post it.
Regards
Jorge
"The dark side clouds everything. Impossible to see the future is."
-
Nov 9th, 2004, 03:41 AM
#15
Hi
VB Code:
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hWnd As Integer, ByRef lpdwProcessId As IntPtr) As IntPtr
Dim currentProcess As Process = Process.GetCurrentProcess()
Dim processId As IntPtr
' we ignore the return value, since we don't care
' about the thread id...
GetWindowThreadProcessId(xlApp.Hwnd, processId)
Dim myExcelPID As Process = Process.GetProcessById(processId.ToInt32())
'' to kill the process
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp)
xlApp = Nothing
'' Verifies if excel is still running if so terminate it
Dim aProcesses() As Process = Process.GetProcesses
Dim aProcess As Process
For y = 0 To aProcesses.GetUpperBound(0)
If aProcesses(y).Id = myExcelPID.Id Then
aProcess = aProcesses(y)
Try
aProcess.Kill()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
Exit For
End If
Next
GC.Collect()
Regards
Jorge
Last edited by Asgorath; Nov 9th, 2004 at 03:44 AM.
"The dark side clouds everything. Impossible to see the future is."
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|