0

I wrote a code that opens a excel file and reads a specific field. The problem is that every time I execute this code, in the background starts an excel process, the user does not see it, but when you start the Task Manager, you see an excel process. I want to close this background process with VBA. I already tried a lot of different things, but nothing really functions. I already tried:

wb.Close
xcelapp.Application.Quit
Set excelapp = Nothing
Set wb = Nothing

Does any one knows how to close excel with vba?

      `Private Sub Command46_Click()
    Dim wert As String

    Dim sfdcID As String
    Dim excelapp As Object
    Dim wb As Object

    wbName = Combo39.Column(0)
    Pfad = "C:\Users\XXXXXX
    Details = "Delivery Input"

    'Open excel
    Set excelapp = CreateObject("excel.application")
    Set wb = excelapp.Workbooks.Open(Pfad)

    'read SFDC Opportunity ID
    Workbooks(wbName).Sheets(Details).Select
    Cells.Find(What:="COMPASS WBS ID").Activate
    ActiveCell.Offset(1, 0).Select
    sfdcID = Selection
    MsgBox sfdcID     

    End Sub`
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Could be answered here... http://stackoverflow.com/questions/18738677/vba-application-quit-command-not-closing-the-entire-excel-application – Aeneas Jan 26 '17 at 14:51
  • Possible duplicate of [VBA Application.Quit command not closing the entire Excel Application](http://stackoverflow.com/questions/18738677/vba-application-quit-command-not-closing-the-entire-excel-application) – Bugs Jan 26 '17 at 14:52
  • I tried both solution, but they did not fix the problem – Daniel Knoesel Jan 27 '17 at 07:21

1 Answers1

0

If you already have an Excel process running, your code will add yet another. Try this:

Sub OpenAndCloseExcel()
    Dim XL As Object        'Excel.Application
    Dim WB As Object        'Excel.Workbooks

    Set XL = GetExcel()

        '... do your thing 

    XL.Quit
    Set XL = Nothing
End Sub

'****************************************
'* Get an Excel instance
'*
Function GetExcel() As Object
    Dim XL As Object        'Excel.Application

    On Error Resume Next

    Set XL = GetObject(, "Excel.Application")
    If XL Is Nothing Then Set XL = CreateObject("Excel.Application")
    Set GetExcel = XL  
End Function