Results 1 to 4 of 4

Thread: How to close an Excel process by itself

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2001
    Posts
    77

    How to close an Excel process by itself

    Dim FN As String = Server.MapPath("\esmp\temp\" & Session.SessionID & ".xls")
    Dim xl As New Excel.Application
    Dim wk As Excel.Workbook = xl.Workbooks.Add()
    xl.Cells(2, 2) = "its ok"
    wk.Close(SaveChanges:=True, FileName:=FN)
    wk = Nothing
    xl.Quit()
    xl = Nothing
    -------------------------------
    that was the coding I wrote in the button click event. It does work fine...but it is creating a separate process every time, the event is fired...and the process from task manager could not get deleted automatically. If I click on button 5 times....5 processes named as "EXCEL.EXE" are staying in the processes of TaskManager.

    So, How to make the process to close byitself after the completion of its work.

  2. #2
    Lively Member tom_hotspur's Avatar
    Join Date
    Aug 2002
    Location
    Stafford
    Posts
    96
    The COM object has not been released properly.
    Microsoft suggest this solution using the
    System.Runtime.InteropServices.Marshal.ReleaseComObject method.


    VB Code:
    1. Private Sub yoursub()
    2. Dim FN As String = Server.MapPath("\esmp\temp\" & Session.SessionID & ".xls")
    3. Dim xl As New Excel.Application
    4. Dim wk As Excel.Workbook = xl.Workbooks.Add()
    5. xl.Cells(2, 2) = "its ok"
    6. wk.Close(SaveChanges:=True, FileName:=FN)
    7. wk = Nothing
    8. xl.Quit()
    9.  
    10. 'use the NAR sub to release COM object
    11. NAR(xl)
    12. End Sub
    13.  
    14.  Private Sub NAR(ByVal o As Object)
    15.             '* Procedure: NAR
    16.             '* Author: Microsoft
    17.             '* Description:  This sub cleanly releases the COM Automation Object.
    18.             '* Source: [url]http://support.microsoft.com/?kbid=317109[/url]
    19.  
    20.             Try
    21.                 System.Runtime.InteropServices.Marshal.ReleaseComObject(o)
    22.             Catch
    23.             Finally
    24.                 o = Nothing
    25.             End Try
    26.         End Sub
    tom

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Aug 2001
    Posts
    77
    Thank you very very much...

  4. #4
    Lively Member tom_hotspur's Avatar
    Join Date
    Aug 2002
    Location
    Stafford
    Posts
    96
    I actually know something!

    Wow... so thats what it feels like.
    tom

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