bd528
bd528

Reputation: 886

Excel process left after closing Excel instance created by Access

I am using the code below to export a SQL statement from Access to Excel :-

Dim myrs        As DAO.Recordset ' Create a recordset to hold the data
Dim myExcel     As New Excel.Application 
Dim mySheet     As Excel.Worksheet

Set mySheet = myExcel.Workbooks.Add(1).Worksheets(1) 
Set myrs = CurrentDb.OpenRecordset("select...") ' 

With mySheet
    .Range("A2").CopyFromRecordset myrs
End With

myExcel.Visible = True
myExcel.WindowState = xlMaximized

Set mySheet = Nothing
Set myExcel = Nothing

This all works perfectly. However when I close Excel, an Excel process is left open. Is there a way to prevent this from happening?

Upvotes: 0

Views: 53

Answers (1)

Gustav
Gustav

Reputation: 55831

Try this - it works for me:

Dim myrs        As DAO.Recordset ' Create a recordset to hold the data
Dim myExcel     As New Excel.Application
Dim myBook      As Excel.Workbook
Dim mySheet     As Excel.Worksheet

Set myBook = myExcel.Workbooks.Add(1)
Set mySheet = myBook.Worksheets(1)
Set myrs = CurrentDb.OpenRecordset("select...") '

With mySheet
    .Range("A2").CopyFromRecordset myrs
End With

myExcel.Visible = True
myExcel.WindowState = xlMaximized

The Excel instance closes when Excel is manually closed.

Upvotes: 1

Related Questions