Results 1 to 4 of 4

Thread: Releasing COM and quitting Excel32.exe

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jun 2012
    Posts
    16

    Releasing COM and quitting Excel32.exe

    Thanks BobRodes for that indepth explaination but as I am very new to VB.net I am still having some problems. I released my com object correclty but excel is still running in the background. Any way of shutting it down after releasing my com objects? and in a relate topic. Am I releasing them correctly? Got this from another thread.
    Code:
    Try
        System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
        obj = Nothing
    Catch ex As Exception
        obj = Nothing
    Finally
        GC.Collect()
    End Try

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Releasing COM and quitting Excel32.exe

    prior to releasing the objects, you need to make sure you .Close your workbook and .Quit the Excel.Application....

    Usually the pattern is like this:
    release worksheet object
    close workboox
    release workbook
    quit Excel
    release Excel

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,713

    Re: Releasing COM and quitting Excel32.exe

    I have a working example Excel automation properly using objects primer which shows how to properly access objects and dispose of them.

    The following code requires a OpenDialog, button and Checkbox. If the CheckBox is unchecked then we access the active worksheet which was the last sheet accessed via opening Excel and selecting a sheet which is not what you really want. If checked you can pass an numeric but suppose you want Sheet1 which has an index of 3 instead of 1? Also xlWorkSheet when passed in a numeric will not release unless calling the GC.

    Code:
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim InitialPath As String = "C:\DotnetLand2010\Excel1\bin\Debug"
    
            OpenFileDialog1.Title = "Please select a file to open"
            OpenFileDialog1.FileName = ""
    
            If IO.Directory.Exists(InitialPath) Then
                OpenFileDialog1.InitialDirectory = InitialPath
            Else
                OpenFileDialog1.InitialDirectory = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
            End If
    
            OpenFileDialog1.Filter = "Excel 2007 (*.xlsx)|*.xlsx|Excel pre 2007|*.xls"
    
            If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
                Dim xlApp As Excel.Application = Nothing
                Dim xlWorkBooks As Excel.Workbooks = Nothing
                Dim xlWorkBook As Excel.Workbook = Nothing
                Dim xlWorkSheet As Excel.Worksheet = Nothing
                xlApp = New Excel.Application
                xlApp.DisplayAlerts = False
                xlWorkBooks = xlApp.Workbooks
                xlWorkBook = xlWorkBooks.Open(OpenFileDialog1.FileName)
    
                xlApp.Visible = False
    
                If CheckBox1.Checked Then
                    xlWorkSheet = CType(xlWorkBook.Sheets(1), Excel.Worksheet)
                Else
                    xlWorkSheet = CType(xlWorkBook.ActiveSheet, Excel.Worksheet)
                End If
    
                Dim xlCells As Excel.Range = Nothing
    
                Dim xlRangeToRemove = xlWorkSheet.Range("A1:B1")
                Dim MyRange = xlRangeToRemove.EntireColumn
                MyRange.Delete(Nothing)
                xlWorkBook.SaveAs(OpenFileDialog1.FileName, Excel.XlFileFormat.xlOpenXMLWorkbook)
                xlWorkBook.Close()
                xlApp.UserControl = True
                xlApp.Quit()
    
                If Not MyRange Is Nothing Then
                    Marshal.FinalReleaseComObject(MyRange)
                    MyRange = Nothing
                End If
    
                If Not xlRangeToRemove Is Nothing Then
                    Marshal.FinalReleaseComObject(xlRangeToRemove)
                    xlRangeToRemove = Nothing
                End If
                If Not xlWorkSheet Is Nothing Then
                    Marshal.FinalReleaseComObject(xlWorkSheet)
                    xlWorkSheet = Nothing
                End If
                If Not xlWorkBook Is Nothing Then
                    Marshal.FinalReleaseComObject(xlWorkBook)
                    xlWorkBook = Nothing
                End If
                If Not xlWorkBooks Is Nothing Then
                    Marshal.FinalReleaseComObject(xlWorkBooks)
                    xlWorkBooks = Nothing
                End If
                If Not xlApp Is Nothing Then
                    Marshal.FinalReleaseComObject(xlApp)
                    xlApp = Nothing
                End If
    
                If CheckBox1.Checked Then
                    GC.Collect()
                    GC.WaitForPendingFinalizers()
                    GC.Collect()
                    GC.WaitForPendingFinalizers()
                End If
                MessageBox.Show("Done")
            End If
        End Sub
    Another thing, if you use more than one "." this will cause objects not to release.

    Wrong
    Code:
    xlWorkSheet.Range("A1").Value
    Correct
    Code:
    Dim SingleCellToRead = xlWorkSheet.Range("A1")
    Dim A1_Value As String = String.Format("A1 = '{0}'", SingleCellToRead.Value)

  4. #4
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Releasing COM and quitting Excel32.exe

    Another link which can help you in interacting with Excel and Cleaning up after you are done. See Point 24 in that link.
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

Tags for this Thread

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