0

I'm attempting to export data from my Access database to an Excel workbook. Everything works fine, except that I can't get Excel to properly shut down after the export is complete. When I open up the Task Manager after the code executed, an instance of Microsoft Excel is always listed under Background Processes. I read various comments suggesting that the issue might be that, somewhere between creating the initial Excel.Application object and attempting to close it, another Excel.Application is secretly being created. As a result, I trimmed the code down to the point where all it does is creating and closing an Excel.Application object. Still the same result.

This is my code:

Sub testexcel()

    Dim xl As Excel.Application

    Set xl = Excel.Application

    xl.Quit

    Set xl = Nothing

End Sub

I'd appreciate any suggestions on what I have to change to get Excel to close properly. Thanks!

mcgurck
  • 53
  • 7
  • 3
    Possible duplicate of [Excel.Application Object .Quit leaves EXCEL.EXE running](https://stackoverflow.com/questions/42113082/excel-application-object-quit-leaves-excel-exe-running) – Ken White Nov 30 '17 at 19:38
  • Similar to https://stackoverflow.com/questions/17777545/closing-excel-application-process-in-c-sharp-after-data-access – n8. Nov 30 '17 at 19:39
  • [This answer](https://stackoverflow.com/a/46082446/7296893) might also be of use – Erik A Nov 30 '17 at 20:16

3 Answers3

1

This code worked for me in the end:

Sub testexcel()

   Dim xl as Excel.Application

   Set xl = New Excel.Application

   'Code

   xl.Quit

   Set xl = Nothing

End Sub

Apparently the 'New' keyword made all the difference.

mcgurck
  • 53
  • 7
0

You state that you're 'attempting to export data from my Access database ' - but you don't state how - - and there is more than 1 way to do this.

In Access the ribbon's External tab has the export feature. If one opens the data set (table or query) on screen and then manually does an export using this feature - the Excel application should not get opened at all. Have you sanity checked via this approach?

Cahaba Data
  • 624
  • 1
  • 4
  • 4
0

Use CreateObject("Excel.Application") instead.

Sub testexcel()

    Dim xl As Excel.Application

    Set xl = CreateObject("Excel.Application")

    Stop    ' so you can see in Task Manager / Process Explorer that EXCEL.EXE is there

    xl.Quit
    Set xl = Nothing
    ' and it's gone

End Sub
Andre
  • 26,751
  • 7
  • 36
  • 80