I have a VBA application in Access that does a lot of spreadsheet manipulation. At the end of the subroutine or function I close the spreadsheet and set all the objects to Nothing. Looking at task manager, as soon as the appliction object is set to Nothing, the Excel instance clears from memory. However, I wanted to add a chart to the spreadsheet. Although the code created the chart, Excel no longer is released from memory at the end of the function as seen in Task Manager. I've searched quite a few message boards, and I believe I'm doing everything correctly as far as using objects for everything, not using the With construct, and setting the objects equal to nothing in the correct order. Below is some sample code, I removed much of it to keep the listing short.
Any help would be greatly appreciated.
Dim exlApp As Excel.Application
Dim exlBook As Excel.Workbook
Dim exlSheet As Excel.Worksheet
Set exlApp = CreateObject("Excel.Application")
Set exlBook = GetObject("c:\junk.xls")
exlApp.Visible = True
exlBook.Windows(1).Visible = True
Set exlSheet = exlBook.Worksheets(1)
exlBook.Sheets(1).Activate
'***************************************************
'add data and format spreadsheet code here
'***************************************************
Dim exlChart As Excel.Chart
Dim exlRange As Excel.Range
Set exlChart = Charts.Add
Set exlRange = exlSheet.Range("A1:E979")
exlChart.ChartType = xlLine
exlChart.SetSourceData exlRange, xlColumns
exlChart.Location xlLocationAsObject, "Sheet1"
exlBook.Save
exlBook.Close
exlApp.Quit
CleanUpAndExit:
If exlRange Is Nothing Then
Else
Set exlRange = Nothing
End If
If exlChart Is Nothing Then
Else
Set exlChart = Nothing
End If
If exlSheet Is Nothing Then
Else
Set exlSheet = Nothing
End If
If exlBook Is Nothing Then
Else
Set exlBook = Nothing
End If
If exlApp Is Nothing Then
Else
Set exlApp = Nothing
End If
Exit Function
Any help would be greatly appreciated.
Dim exlApp As Excel.Application
Dim exlBook As Excel.Workbook
Dim exlSheet As Excel.Worksheet
Set exlApp = CreateObject("Excel.Application")
Set exlBook = GetObject("c:\junk.xls")
exlApp.Visible = True
exlBook.Windows(1).Visible = True
Set exlSheet = exlBook.Worksheets(1)
exlBook.Sheets(1).Activate
'***************************************************
'add data and format spreadsheet code here
'***************************************************
Dim exlChart As Excel.Chart
Dim exlRange As Excel.Range
Set exlChart = Charts.Add
Set exlRange = exlSheet.Range("A1:E979")
exlChart.ChartType = xlLine
exlChart.SetSourceData exlRange, xlColumns
exlChart.Location xlLocationAsObject, "Sheet1"
exlBook.Save
exlBook.Close
exlApp.Quit
CleanUpAndExit:
If exlRange Is Nothing Then
Else
Set exlRange = Nothing
End If
If exlChart Is Nothing Then
Else
Set exlChart = Nothing
End If
If exlSheet Is Nothing Then
Else
Set exlSheet = Nothing
End If
If exlBook Is Nothing Then
Else
Set exlBook = Nothing
End If
If exlApp Is Nothing Then
Else
Set exlApp = Nothing
End If
Exit Function