Closing an already open excel file from access vba (1 Viewer)

stylo9

New member
Local time
Today, 06:13
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:

stylo9

New member
Local time
Today, 06:13
Joined
May 25, 2015
Messages
5
Thanks vbaInet
That worked, I'm not sure exactly what it was that I did with getobject() but it worked so I'm grateful
 

vbaInet

AWF VIP
Local time
Today, 05:13
Joined
Jan 22, 2010
Messages
26,374
You obviously did it right ;)

Welcome to the forum by the way.
 

Users who are viewing this thread

Top Bottom