Wrox Home  
Search P2P Archive for: Go

  Return to Index  

access thread: Closing Excel object from Access


Message #1 by "Charles Moore" <charles.Moore@d...> on Wed, 27 Nov 2002 15:11:06
I open an Excel object from a form within Access and it displays &  prints 
etc as required.  However, when I close it down from Excel the Excel 
application remains in memory until I close it down in the Task Manager.  
It also goes away if I  open the VBE in Access, go to the module 
containing the code and click on the stop button.   As you might imagine, 
I don't want the users to do either of these things.

Any ideas anyone?  I'm a relative novice so it's likely to be something 
really stupid.
Message #2 by "Bob Bedell" <bobbedell15@m...> on Wed, 27 Nov 2002 15:52:49 +0000
Hi Charles,

Try this. To exit Excel from Access, declare a global object variable
in an Access standard module to hold a reference to your instance of
Excel:

Public gobjExcel As Excel.Application

Use the global object variable to instantiate Excel:

Set gobjExcel = New Excel.Application

Add your Workbook, format your Worksheets, etc:

gobjExcel.Workbooks.Add

When you are finished with your processing and destroy your other object 
variables (worksheets, recordsets, etc.), do not set gobjExcel to Nothing. 
While your instance of Excel is up and running, gobjExcel continues to hold 
a reference to the open Excel workbook in your Access standard module.

Now paste the following sub procedure in the standard module where you 
declared your global object variable:

Sub CloseExcel()

    If Not gobjExcel Is Nothing Then
        gobjExcel.DisplayAlerts = False
        gobjExcel.Quit
    End If

CloseExcel_Exit:
    Set gobjExcel = Nothing
    Exit Sub

CloseExcel_Err:
    MsgBox "Error # " & Err.Number & ": " & Err.Description
    Resume CloseExcel_Exit
End Sub

To close Excel from Access, place a command button on your form, and call 
the above sub procedure:

Private Sub cmdExitExcel_Click()
    Call CloseExcel
End Sub

Hope that works?

Bob

>From: "Charles Moore" <charles.Moore@d...>
>Reply-To: "Access" <access@p...>
>To: "Access" <access@p...>
>Subject: [access] Closing Excel object from Access
>Date: Wed, 27 Nov 2002 15:11:06
>
>I open an Excel object from a form within Access and it displays &  prints
>etc as required.  However, when I close it down from Excel the Excel
>application remains in memory until I close it down in the Task Manager.
>It also goes away if I  open the VBE in Access, go to the module
>containing the code and click on the stop button.   As you might imagine,
>I don't want the users to do either of these things.
>
>Any ideas anyone?  I'm a relative novice so it's likely to be something
>really stupid.


_________________________________________________________________
MSN 8 with e-mail virus protection service: 2 months FREE* 
http://join.msn.com/?page=features/virus


  Return to Index