Excel not closing!

Hi. When I create a workbook then add some sheets I am finding that the excel process is not closing in task manager. In the code below, I create a series of workbooks, then add sheets. Finally later I will need to populate data. There will be approx 200 workbooks and up to 10 sheets per workbook so it is important that I close each one after each step.

The problem I am getting is that the workbooks are staying open. What have I done wrong?


    Private Sub AddSheet(ByVal folder As String, ByVal workbook As String, ByVal sheet As String)
        Dim xlObj As Object

        xlObj = CreateObject("Excel.Application")
        xlObj.visible = False

        Dim wbk As Object
        wbk = xlObj.Workbooks.Open(Filename:=folder & workbook, UpdateLinks:=False, ReadOnly:=False)
        mysheet = wbk.sheets.add()
        mysheet.name = sheet

        xlObj.Workbooks.Close()
        wbk.saveas(folder & workbook)
        wbk.Close()
        xlObj.Quit()
        xlObj = Nothing
        wbk = Nothing
    End Sub



    Private Function CreateWorkbook(ByVal folder As String, ByVal workbook As String) As Object
        Dim xlObj As Object

        xlObj = CreateObject("Excel.Application")
        xlObj.visible = False
        xlObj.workbooks.add()
        sheetCount = xlObj.workbooks(1).sheets.count
        xlObj.workbooks(1).saveas(folder & workbook)
        xlObj.Workbooks.Close()
        xlObj.Quit()
        xlObj = Nothing
    End Function



LVL 1
craigdevAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

bruintjeCommented:
Hello craigdev,

you could have a look at this comrelesae function
source: http://www.codeproject.com/vb/net/ReleaseExcelObject.asp
---------
Private Sub ReleaseComObject(ByRef Reference As Object)
    Try        
        Do Until _
         System.Runtime.InteropServices.Marshal.ReleaseComObject(Reference)<=0
        Loop
    Catch
    Finally
        Reference = Nothing
    End Try
End Sub
---------

and apply it to your code like
---------
    Private Sub AddSheet(ByVal folder As String, ByVal workbook As String, ByVal sheet As String)
        Dim xlObj As Object

        xlObj = CreateObject("Excel.Application")
        xlObj.visible = False

        Dim wbk As Object
        wbk = xlObj.Workbooks.Open(Filename:=folder & workbook, UpdateLinks:=False, ReadOnly:=False)

        Dim mysheet As Object
        mysheet = wbk.sheets.add()
        mysheet.name = sheet

        wbk.saveas(folder & workbook)
        wbk.Close()

        'cleanup
        mysheet = Nothing
        ReleaseComObject(mysheet)
        wbk = Nothing
        ReleaseComObject(wbk)
        xlObj.Quit()
        ReleaseComObject(xlObj)
    End Sub
---------

hope this helps a bit
bruintje

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
craigdevAuthor Commented:
Thanks again
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.