Solved

Cleanning up after Excel Object

Posted on 2012-03-27
4
427 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
Comment Utility
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
Comment Utility
If i do, i get at Line 36 re Cells:
Sub or function not defined.
0
 

Author Comment

by:APD_Toronto
Comment Utility
Why can't i clean up Excel *32
0
 

Author Comment

by:APD_Toronto
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now