Home » excel » VBA Closing Excel Files

VBA Closing Excel Files

Posted by: admin April 23, 2020 Leave a comment

Questions:

Say I have some VBA code in Access that uses excel for whatever reason. Sometimes I have trouble closing the file properly.

The code (I think) should look somewhat like this:

WBO.Close savechanges:=True
Set WBO = Nothing

XLO.Application.Quit
Set XLO = Nothing

rs.Close
db.Close

Here XLO is an excel object, WBO is a workbook object, rs is a DAO recordset and db is a DAO database. Even though the physical excel file closes there’s still an “EXCEL.EXE” process occurring on my system, which stops me from running my program twice in a row. Does anyone know why this happens?

EDIT

I took out the rs.Close and then db.Close lines since I decided to manually export the data I was using from Access to Excel (there’s too many rows and columns to copy each cell over efficiently). However, this didn’t change the problem at hand.

Thank you,

Jesse Smothermon

How to&Answers:

There isn’t an actual Excel object. Application is the top-level object but that Application object has an Application property which actually points to “the creator of the specified object”. So trying to quit XLO.Application won’t do what you think it should.

Presuming that XLO is of type Excel.Application then try just XLO.Quit instead of XLO.Application.Quit

Answer:

Why not just:

application.displayalerts = false
WBO.save (or saveas whatever it matters)
WBO.close 
set WBO = nothing 

I don’t see a memory problem with that…

Answer:

Refer to this answer.
It worked for me.

Dim sKill As String

sKill = "TASKKILL /F /IM msaccess.exe"
Shell sKill, vbHide

If that works, mark the answer in the link with a ^, not mine.