Code for closing an Excel file from Access

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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
 
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?"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top