Closing an already open excel file from access vba

stylo9

New member
Local time
Today, 19:09
Joined
May 25, 2015
Messages
5
Hi I've tried numerous ways of doing this which are mostly copied from this forum and nothing seems to work.
My problem is that I want to delete an excel file from within access vba.
problem is that if the file is open, it obviously cannot be deleted. so I need to check to see if that specific excel workbook is open and if it is, I want to close it before I can delete it.
all of the answers here involve creating an excel object and opening it before closing it. If I do that all that happens is a second instance of the workbook opens then closes leaving the originally open workbook still open.

Sub xx()
Dim XLapp As New Excel.Application
Dim ObjXL As Excel.Workbook
Set ObjXL = XLapp.Workbooks.Open("C:\dropbox\excel\import.xlsx")
ObjXL.Application.Visible = True
ObjXL.Windows(1).Visible = True
ObjXL.Worksheets(1).Activate
'ObjXL.Save
ObjXL.Close True
XLapp.Quit
End Sub

The above code demonstrates my problem, if the import.xlsx file is already open then the code just opens another instance of this workbook and then closes it again leaving the original workbook open and as such cannot be deleted.
 
Last edited:
Thanks vbaInet
That worked, I'm not sure exactly what it was that I did with getobject() but it worked so I'm grateful
 
You obviously did it right ;)

Welcome to the forum by the way.
 

Users who are viewing this thread

Back
Top Bottom