• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 455
  • Last Modified:

Cleanning up after Excel Object

hi Experts

I have the below code in Access, and running it once works, but not repeatedly.  I hav also noticed that under Task Manager, when running this code my number of proccess go up by 4, but go down only by 3, leaving EXCEL.EXE *32 listed.

I'm setting my 2 excel objects to nothing, why it isn't cleanning up?

    Dim strFilePath As String
    Dim intRowCount, intRow As Integer
    Dim intColCount, intCol As Integer
    Dim objXL As Excel.Application
    Dim objSheet As Excel.Worksheet
    Dim fso
    Set fso = CreateObject("Scripting.FileSystemObject")
    strFilePath = Environ("userProfile") & "\Desktop\SalesByProduct.xls"
    If fso.FileExists(strFilePath) Then Kill strFilePath
    Set fso = Nothing
    DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel8, "qryProdTotals", strFilePath, True
    Set objXL = CreateObject("Excel.Application")
    objXL.Workbooks.Open strFilePath
    Set objSheet = objXL.ActiveWorkbook.Worksheets("qryProdTotals")
    intRowCount = objSheet.UsedRange.Rows.Count
    intColCount = objSheet.UsedRange.Columns.Count
    objSheet.Cells(intRowCount + 2, 1).Select
    With objXL.ActiveCell
        .Value = "T O T A L S :"
        .Font.Bold = True
        .Interior.ColorIndex = 16
    End With
    For intCol = 2 To intColCount
        objSheet.Cells(intRowCount + 2, intCol).Select
        With objXL.ActiveCell
            .Value = objXL.WorksheetFunction.Sum(Range(Cells(2, intCol), Cells(intRowCount, intCol)))
            .Font.Bold = True
            .Interior.ColorIndex = 16
        End With
    'Hilight Row1
    'Format col2...colMax, Row...rowMax+2
    Set objSheet = Nothing
    Set objXL = Nothing
    MsgBox "Report Exported to " & strFilePath

'***Export to Excel END***

Open in new window

APD Toronto
APD Toronto
  • 3
1 Solution
I've tested it in Windows 7 32 bit, Access 2010 and all is working without problems. Excel is  unloaded. Only one comment: you have late binding of Excel an it will be better to declare your variables as:
    Dim objXL As Object
    Dim objSheet As Object
In thhis case you will need no reference to Excel library.
APD TorontoAuthor Commented:
If i do, i get at Line 36 re Cells:
Sub or function not defined.
APD TorontoAuthor Commented:
Why can't i clean up Excel *32
APD TorontoAuthor Commented:
I keep errorring on Li 36.  Now with my code, before Li 56 I added:
 Shell "TaskKill /F /T /IM Excel.exe", vbHide

First execution works great, second at Li36 I get
The remote server machine does not exist or is unavailable
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get 10% Off Your First Squarespace Website

Ready to showcase your work, publish content or promote your business online? With Squarespace’s award-winning templates and 24/7 customer service, getting started is simple. Head to Squarespace.com and use offer code ‘EXPERTS’ to get 10% off your first purchase.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now