Solved

Cleanning up after Excel Object

Posted on 2012-03-27
4
435 Views
Last Modified: 2012-06-21
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
    
    Next
    
    'Hilight Row1
    
    
    'Format col2...colMax, Row...rowMax+2
    
    objXL.ActiveWorkbook.Save
    objXL.Quit
    
    Set objSheet = Nothing
    Set objXL = Nothing
    MsgBox "Report Exported to " & strFilePath
    

'***Export to Excel END***

Open in new window

0
Comment
Question by:APD_Toronto
  • 3
4 Comments
 
LVL 39

Accepted Solution

by:
als315 earned 500 total points
ID: 37775206
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.
0
 

Author Comment

by:APD_Toronto
ID: 37777001
If i do, i get at Line 36 re Cells:
Sub or function not defined.
0
 

Author Comment

by:APD_Toronto
ID: 37777008
Why can't i clean up Excel *32
0
 

Author Comment

by:APD_Toronto
ID: 37777249
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
0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…

773 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