Results 1 to 15 of 15

Thread: Problems with Excel Interop

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2002
    Location
    Reading
    Posts
    70

    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

  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jun 2002
    Location
    Reading
    Posts
    70
    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

  4. #4
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Hmm post your code. I have used Excel Interop without this problem.

  5. #5
    Fanatic Member
    Join Date
    Oct 2001
    Location
    Indiana
    Posts
    612
    I know this is an old post, but I have the same problem.
    VB Code:
    1. Dim xlApp As New Excel.Application
    2.         Dim xlBook As Excel.Workbook
    3.  
    4.         Dim strXLFile As String = "C:\inetpub\wwwroot\mcclure\" & Left(ListBox2.SelectedItem.Text, 11)
    5.  
    6.         xlBook = xlApp.Workbooks.Open(strXLFile)
    7.  
    8.         xlBook.Close(savechanges:=False)
    9.         xlBook = Nothing
    10.         xlApp.Quit()
    11.         xlApp = Nothing
    Excel is left running in the background.

    Thanks
    David Wilhelm

  6. #6
    Frenzied Member Asgorath's Avatar
    Join Date
    Sep 2004
    Location
    Saturn
    Posts
    2,036
    Hi
    Try this before you set your xlapp to nothing
    VB Code:
    1. System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp)


    Regards
    Jorge
    "The dark side clouds everything. Impossible to see the future is."

  7. #7
    Fanatic Member
    Join Date
    Oct 2001
    Location
    Indiana
    Posts
    612
    I gave it a shot and Excel is still not closing down.
    VB Code:
    1. Dim xlApp As New Excel.Application
    2.         Dim xlBook As Excel.Workbook
    3.  
    4.         Dim strXLFile As String = "C:\inetpub\wwwroot\mcclure\" & Left(ListBox2.SelectedItem.Text, 11)
    5.  
    6.         xlBook = xlApp.Workbooks.Open(strXLFile)
    7.  
    8.         xlBook.Close(savechanges:=False)
    9.         xlBook = Nothing
    10.         xlApp.Quit()
    11.         System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp)
    12.         xlApp = Nothing
    Would it matter that this is in web application?
    David Wilhelm

  8. #8
    Frenzied Member Asgorath's Avatar
    Join Date
    Sep 2004
    Location
    Saturn
    Posts
    2,036
    Originally posted by indydavid32
    I gave it a shot and Excel is still not closing down.
    VB Code:
    1. Dim xlApp As New Excel.Application
    2.         Dim xlBook As Excel.Workbook
    3.  
    4.         Dim strXLFile As String = "C:\inetpub\wwwroot\mcclure\" & Left(ListBox2.SelectedItem.Text, 11)
    5.  
    6.         xlBook = xlApp.Workbooks.Open(strXLFile)
    7.  
    8.         xlBook.Close(savechanges:=False)
    9.         xlBook = Nothing
    10.         xlApp.Quit()
    11.         System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp)
    12.         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."

  9. #9
    Fanatic Member
    Join Date
    Oct 2001
    Location
    Indiana
    Posts
    612
    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.
    David Wilhelm

  10. #10
    Frenzied Member Asgorath's Avatar
    Join Date
    Sep 2004
    Location
    Saturn
    Posts
    2,036
    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."

  11. #11
    Fanatic Member
    Join Date
    Oct 2001
    Location
    Indiana
    Posts
    612
    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:
    1. Dim proc As Process
    2.         For Each proc In Process.GetProcesses
    3.             If UCase(proc.ProcessName) = "EXCEL" Then
    4.                 proc.Kill()
    5.             End If
    6.         Next
    David Wilhelm

  12. #12
    Frenzied Member Asgorath's Avatar
    Join Date
    Sep 2004
    Location
    Saturn
    Posts
    2,036
    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."

  13. #13
    Fanatic Member
    Join Date
    Oct 2001
    Location
    Indiana
    Posts
    612
    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
    David Wilhelm

  14. #14
    Frenzied Member Asgorath's Avatar
    Join Date
    Sep 2004
    Location
    Saturn
    Posts
    2,036
    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."

  15. #15
    Frenzied Member Asgorath's Avatar
    Join Date
    Sep 2004
    Location
    Saturn
    Posts
    2,036
    Hi

    VB Code:
    1. Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hWnd As Integer, ByRef lpdwProcessId As IntPtr) As IntPtr
    2.  
    3. Dim currentProcess As Process = Process.GetCurrentProcess()
    4. Dim processId As IntPtr
    5.  
    6.         ' we ignore the return value, since we don't care
    7.         ' about the thread id...
    8.  
    9. GetWindowThreadProcessId(xlApp.Hwnd, processId)
    10.  
    11. Dim myExcelPID As Process = Process.GetProcessById(processId.ToInt32())
    12.  
    13. '' to kill the process
    14.         System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp)
    15.         xlApp = Nothing
    16.  
    17.  
    18.         '' Verifies if excel is still running if so terminate it
    19.  
    20.         Dim aProcesses() As Process = Process.GetProcesses
    21.         Dim aProcess As Process
    22.  
    23.         For y = 0 To aProcesses.GetUpperBound(0)
    24.  
    25.             If aProcesses(y).Id = myExcelPID.Id Then
    26.                 aProcess = aProcesses(y)
    27.                 Try
    28.                     aProcess.Kill()
    29.                 Catch ex As Exception
    30.                     MessageBox.Show(ex.Message)
    31.                 End Try
    32.                 Exit For
    33.             End If
    34.         Next
    35.  
    36.         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
  •  



Click Here to Expand Forum to Full Width