0

I am opening Excel from MsAccess and when I finished the task I close it, but Excel remain opened in backgroud until I close MsAccess

this is the code

Public Sub closeExcel()
    Dim xlApp
    Dim wkb     As Object
    Dim wks     As Object
    
    On Error Resume Next
    Set xlApp = GetObject(, "Excel.Application")

    If err <> 0 Then
        Set xlApp = CreateObject("Excel.Application")
    End If

    With xlApp
        Set wkb = .Workbooks.Add
        Set wks = wkb.Worksheets(1)

        wkb.Close True
        Set wks = Nothing
        Set wkb = Nothing
        .Quit

    End With
End Sub

I need to remain Access runing, but I want that Excel be closed. How can I do this?

DS_London
  • 3,644
  • 1
  • 7
  • 24

1 Answers1

0

You are cutting off the worksheet. Try this:

With xlApp
    Set wkb = .Workbooks.Add
    Set wks = wkb.Worksheets(1)
    ' Do stuff.
    Set wks = Nothing
    wkb.Close True
    Set wkb = Nothing
    .Quit
End With

Set xlApp = Nothing
Gustav
  • 53,498
  • 7
  • 29
  • 55