Results 1 to 9 of 9

Thread: Close excel instance after creating .xls file in VB

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Oct 2000
    Posts
    1,463

    Question Close excel instance after creating .xls file in VB

    Hi Everyone,

    I am creating an excel file in my vb app. Below is the code I am using to do this. But when the application is closed, I still have an instance of Excel running in the background. How do i close it? I can do it with the xlWBook but not the xlApp. Thanks!

    Code:
                    'create empty excel file
                    xlApp = CType(CreateObject("Excel.Application"), Microsoft.Office.Interop.Excel.Application)
                    xlWBook = CType(xlApp.Workbooks.Add, Microsoft.Office.Interop.Excel.Workbook)
                    xlWSheet = CType(xlWBook.Worksheets(1), Microsoft.Office.Interop.Excel.Worksheet)
    
    
    ... do stuff here
    
    
                xlWSheet.SaveAs(sFilePath)
                xlWBook.Close()
    How do I close to get rid of xlApp?

    Thanks!

  2. #2

    Thread Starter
    Frenzied Member
    Join Date
    Oct 2000
    Posts
    1,463

    Re: Close excel instance after creating .xls file in VB

    I already did .Quit() but in the Task Manager I still have EXCEL.EXE in there which was created by the vb.net app.

  3. #3
    Frenzied Member
    Join Date
    Jul 2006
    Location
    MI
    Posts
    2,012

    Re: Close excel instance after creating .xls file in VB

    I use this...

    Code:
    System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp)

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

    Re: Close excel instance after creating .xls file in VB

    Add this

    vb Code:
    1. xlWBook= Nothing
    2. xlApp.Quit()
    3. xlApp= Nothing

    After

    xlWBook.Close()
    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

  5. #5
    Fanatic Member
    Join Date
    Aug 2006
    Location
    In my head
    Posts
    913

    Re: Close excel instance after creating .xls file in VB

    Nbrege has it right. You need to use the Marshal.ReleaseObject method. However, you will have to use it on every object you created (worksheets, workbooks, application) to make sure that everything shuts down properly. While typically just doing it on the App does the trick, sometimes Excel gets an attitude and doesnt want to play nice. Releasing all of the objects gives Excel a bit less of a temper....

    D
    Platforms of choice: Visual Studio 2005/2008 Professional : Visual Studio 2010 Enterprise : PHP - Notepad++/WAMP

    Please Rate If I helped you.
    Please remember to mark threads as closed if your issue has been resolved.

    Reserved Words in Access | Connection Strings

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Close excel instance after creating .xls file in VB

    I was searching for a better way to close an excel application, and found this thread. Unfortunately, what nbrege suggested doesn't work in my case. My Excel app instance is created using late binding in a class. I was using that class to do some exporting in a BackgroundWorker thread. When the exporting was in the UI thread, then xlApp.Quit was sufficient. Now that the export is in a different thread, xlApp.Quit and ReleaseComObject(xlApp) do the same thing: Nothing. In both cases, there are no errors, no complaints, no objections, yet after the methods have executed (I stepped through that code, just to be as sure as I could be), Excel is still alive and kicking.

    Any definitive solution?
    My usual boring signature: Nothing

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Close excel instance after creating .xls file in VB

    The solution is GC.Collect.

    Without adding that, none of what was suggested in this thread was sufficient. However, there are times when that GC.Collect is not necessary. In my case, some object was holding a reference and preventing Excel from closing, even though the object in question was garbage.
    My usual boring signature: Nothing

  8. #8
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Re: Close excel instance after creating .xls file in VB

    Shaggy: I tried this in my code and it excel closed a few seconds after the RunWorkerCompleted event fired. I found that Excel.exe stays open until I close my app, unless I explicitly call the Garbage Collector. Odds are my test app is so small the that garbage collector doesn't fire while it is running.

    Code:
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            BackgroundWorker1.RunWorkerAsync()
        End Sub
    
        Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
            Dim xApp As New Excel.Application
            Dim xWB As Excel.Workbook = xApp.Workbooks.Add()
            Dim xSheet As Excel.Worksheet = DirectCast(xWB.Sheets.Add(), Excel.Worksheet)
    
            xSheet.Cells(1, 1) = "A"
    
            xWB.SaveAs("C:\Temp\testout.xls")
            xApp.Quit()
    
            Marshal.ReleaseComObject(xSheet)
            Marshal.ReleaseComObject(xWB)
            Marshal.ReleaseComObject(xApp)
    
            xSheet = Nothing
            xWB = Nothing
            xApp = Nothing
    
    
    
        End Sub
    
        Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
            GC.Collect()
        End Sub

  9. #9
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Close excel instance after creating .xls file in VB

    Yeah, I had to explicitly collect the garbage, as well, though I had a suitable location a bit after the thread closed.
    My usual boring signature: Nothing

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