|
|
Excel Object ProblemsQuestionI currently have a database which has an export feature. This export feature
exports a ton of Financial data to an Excel Spreadsheet. This is working well
except for one problem. Dim appExcel As Excel.Application
Dim PhaseWB As Workbook
Dim PhaseWS As Worksheet
' Here is what I am using to Open them
Set appExcel = CreateObject("Excel.Application")
Set PhaseWB = appExcel.Workbooks.Open(SSPath, , False, , , , True)
Set PhaseWS = PhaseWB.Sheets("Sheet1")
PhaseWS.Copy before:=Worksheets(1) ' <-- This is where the problem is on the second export.
Set PhaseWS = PhaseWB.Sheets(1)
' When I have finished with the objects this is how I am closing them
PhaseWB.Close SaveChanges:=True
appExcel.Quit
Set appExcel = Nothing
Set PhaseWB = Nothing
Set PhaseWS = Nothing
Does anyone see anything wrong with this? Is there a better way to release Objects/Variables from memory? Any Help anyone can offer would be greatly appreciated. AnswerChris, Dim appExcel As Excel.Application
Dim PhaseWB As Workbook
Dim PhaseWS As Worksheet
Set appExcel = CreateObject("Excel.Application")
Set PhaseWB = appExcel.Workbooks.Open(SSPath, , False, , , , True)
Set PhaseWS = PhaseWB.Sheets("Sheet1")
PhaseWS.Copy before:=PhaseWB.Worksheets(1)
Set PhaseWS = PhaseWB.Sheets(1)
PhaseWB.Close SaveChanges:=True
appExcel.Quit
Set appExcel = Nothing
Set PhaseWB = Nothing
Set PhaseWS = Nothing
But I'd better write it this way (more difference): Dim appExcel As Excel.Application
Dim PhaseWB As Excel.Workbook
Dim PhaseWS As Excel.Worksheet
Set appExcel = CreateObject("Excel.Application")
Set PhaseWB = appExcel.Workbooks.Open(SSPath, , False, , , , True)
Set PhaseWS = PhaseWB.Worksheets("Sheet1")
PhaseWS.Copy before:=PhaseWB.Worksheets(1)
Set PhaseWS = PhaseWB.Worksheets(1)
PhaseWB.Close SaveChanges:=True
Set PhaseWS = Nothing
Set PhaseWB = Nothing
appExcel.Quit
Set appExcel = Nothing
HTH, Copyright © 1999-2008 by Shamil Salakhetdinov. Original version is published here All rights reserved. |