?
Solved

Cleanning up after Excel Object

Posted on 2012-03-27
4
Medium Priority
?
448 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 40

Accepted Solution

by:
als315 earned 2000 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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses
Course of the Month11 days, 18 hours left to enroll

752 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