?
Solved

I need help closing an Excel object in my ASP.Net application

Posted on 2011-05-09
12
Medium Priority
?
267 Views
Last Modified: 2012-06-22
Hi Experts,
In my ASP.Net 3.5 application I open and close an Excel object like in my code below:  

'Open Excel object
        objXL = New Microsoft.Office.Interop.Excel.Application
        objWkB = objXL.Workbooks.Open(strLastQtrPSBD12)
        objXL.Visible = False

'close Excel object
        objWkB.Close(False)
        objXL.Quit()

        objRange = Nothing
        objWkB = Nothing
        objXL = Nothing

I want to be make sure that I close my Excel object when there is an Exception error.  How do I check
If my Excel object is opened, so that I may close it?

Thanks in advance,
mrotor


0
Comment
Question by:mainrotor
  • 6
  • 3
  • 2
  • +1
12 Comments
 
LVL 18

Expert Comment

by:dj_alik
ID: 35722126
''Releases COM objectsImports System.Runtime.InteropServices     ''Releases COM objects    Private Sub ReleaseComponent(ByVal component As Object)        Dim count As Integer = 0         Do            count = Marshal.ReleaseComObject(component)        Loop While count <> 0    End Sub''Releases COM objects
Imports System.Runtime.InteropServices

    ''Releases COM objects
    Private Sub <strong class="highlight">ReleaseComponent</strong>(ByVal component As Object)
        Dim count As Integer = 0

        Do
            count = Marshal.ReleaseComObject(component)
        Loop While count <> 0
    End Sub

Open in new window


Public Sub CloseExcel(ByVal app As Excel.Application, _        ByVal interact As Boolean)         Dim wbk As Excel.Workbook        Dim wrk As Excel.Worksheet         app.Interactive = Interact        app.DisplayAlerts = Interact         If Not app.Workbooks Is Nothing Then            For Each wbk In app.Workbooks                For Each wrk In wbk.Worksheets                    wrk = Nothing                Next                wbk.Close(False)                wbk = Nothing            Next        End If        app.Quit()    End Sub    Public Sub CloseExcel(ByVal app As Excel.Application, _
        ByVal interact As Boolean)

        Dim wbk As Excel.Workbook
        Dim wrk As Excel.Worksheet

        app.Interactive = Interact
        app.DisplayAlerts = Interact

        If Not app.Workbooks Is Nothing Then
            For Each wbk In app.Workbooks
                For Each wrk In wbk.Worksheets
                    wrk = Nothing
                Next
                wbk.Close(False)
                wbk = Nothing
            Next
        End If
        app.Quit()
    End Sub

Open in new window

0
 
LVL 18

Expert Comment

by:dj_alik
ID: 35722131
''Releases COM objects
Imports System.Runtime.InteropServices

    ''Releases COM objects
    Private Sub <strong class="highlight">ReleaseComponent</strong>(ByVal component As Object)
        Dim count As Integer = 0

        Do
            count = Marshal.ReleaseComObject(component)
        Loop While count <> 0
    End Sub

Open in new window

0
 
LVL 18

Expert Comment

by:dj_alik
ID: 35722138
Sorry:
''Releases COM objects
Imports System.Runtime.InteropServices

    ''Releases COM objects
    Private Sub ReleaseComponent(ByVal component As Object)
        Dim count As Integer = 0

        Do
            count = Marshal.ReleaseComObject(component)
        Loop While count <> 0
    End Sub

Open in new window

0
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
LVL 18

Expert Comment

by:dj_alik
ID: 35722145
Example:
xlapp.Quit()
'ss xlApp.UserControl = True
'ss xlApp.Visible = True

'Attempt to close excel - notorious problem with .net not closing
all instances of Excel in task manager!!
System.Runtime.InteropServices.Marshal.ReleaseComO bject(xlSheet)
System.Runtime.InteropServices.Marshal.ReleaseComO bject(wb)
System.Runtime.InteropServices.Marshal.ReleaseComO bject(xlapp)
xlapp = Nothing
wb = Nothing
xlSheet = Nothing
0
 
LVL 17

Expert Comment

by:Carlos Villegas
ID: 35722315
Hi, if you want to effectively close the excel.exe instance, you need to call:
GC.Collect()

Open in new window

after call:
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(...)

Open in new window


Example:
objXL = New Microsoft.Office.Interop.Excel.Application
objWkB = objXL.Workbooks.Open(strLastQtrPSBD12)
objXL.Visible = False

'close Excel object
objWkB.Close(False)
objXL.Quit()

'Mark instances to finallize
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(objWkB)
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(objXL)

objRange = Nothing
objWkB = Nothing
objXL = Nothing

'This will close your Excel instance
GC.Collect()

Open in new window

0
 
LVL 17

Expert Comment

by:Carlos Villegas
ID: 35722396
This is a better example of how to handle your Excel instance inside a try/catch block to effectively close it if an exception is raised:
Dim strLastQtrPSBD12 As String = "SheetName"
Dim objXL As Microsoft.Office.Interop.Excel.Application = Nothing
Dim objWkB As Microsoft.Office.Interop.Excel.Workbook = Nothing

Try
    objXL = New Microsoft.Office.Interop.Excel.Application
    objWkB = objXL.Workbooks.Open(strLastQtrPSBD12)
    objXL.Visible = False

    'close Excel object
    objWkB.Close(False)

    System.Runtime.InteropServices.Marshal.FinalReleaseComObject(objWkB)
Catch ex As Exception
    'Log error???
Finally
    objXL.Quit()
    System.Runtime.InteropServices.Marshal.FinalReleaseComObject(objXL)

    'This will close your Excel.exe process
    GC.Collect()
End Try

Open in new window

0
 
LVL 18

Expert Comment

by:dj_alik
ID: 35722410
very good example by yv989c
0
 
LVL 17

Expert Comment

by:Carlos Villegas
ID: 35722637
@dj_alik thanks buddy, not long ago I passed through this.
0
 
LVL 13

Accepted Solution

by:
Rick earned 2000 total points
ID: 35722901
Dim pr() As Process = Process.GetProcessesByName("excel")

For Each p As Process In pr
       p.CloseMainWindow()
Next
0
 
LVL 18

Expert Comment

by:dj_alik
ID: 35722987
rick_gwu's solution  good for  server side
0
 

Author Comment

by:mainrotor
ID: 35786475
rick_gwu,
I followed your example, but first I check for the process id created when my Excel object was opened.  Then I make sure to only close the correct process.
0
 
LVL 13

Expert Comment

by:Rick
ID: 35787970
Good!

Glad to help.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses
Course of the Month16 days, 4 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question