Code for closing an Excel file from Access

M

Matt

I have a form that opens an Excel file called "Test.exl". I want to be able
to close this file using some kind of code and unfortunately I am not sure
how to do it. Can anyone provide a method that will automatically close and
save without prompt the file?


thanks,
Matt
 
M

Mike Labosh

You will need to make an event procedure for a button, add a reference to
Microsoft Excel (Tools -> References in the VBA window) and use code similar
to this below.

You will need to look at the help topic for the GetObject function, and test
your stuff carefully, because if a user has several instances of Excel
running, the GetObject function just randomly picks one.

Public Sub CloseExcel()

On Error GoTo CloseExcelError

Dim exl As Excel.Application

Set exl = GetObject(, "Excel.Application")
exl.Quit

CloseExcelExit:
Set exl = Nothing
Exit Sub

CloseExcelError:

If Err.Number = 429 Then
MsgBox "Excel is not running right now.", vbInformation, "Closing
Excel:"
Else
MsgBox Err.Description, vbCritical, "Closing Excel:"
End If

Resume CloseExcelExit

End Sub


--
Peace & happy computing,

Mike Labosh, MCSD

"It's 4:30 am. Do you know where your stack pointer is?"
 
Top