Late Binding Excel 2010 Leaving Process Behind (1 Viewer)

cable

Access For My Sins
Local time
Today, 12:29
Joined
Mar 11, 2002
Messages
228
I've got an issue where my late binding between Access 2010 VBA and Excel 2010 is leaving a Excel.exe process behind :(

I'm creating the objects via:
Code:
Dim wbExcel As Object, wsSheet As Object

'Open the workbook, pick the worksheet and open it
On Error Resume Next
Set wbExcel = GetObject(, "Excel.Application")
If Err.Number <> 0 Then
    'Could not get instance, so create a new one
    Err.Clear
    On Error GoTo Error_Handler
    Set wbExcel = CreateObject("Excel.Application")
Else
    On Error GoTo Error_Handler
End If
    
wbExcel.WorkBooks.Open (sExcelFile)
Set wsSheet = wbExcel.Worksheets.Item("Score Sheet")

In the middle I'm only ever reading from the multiple sheet workbook.

And when closing/exiting the sub I've got:
Code:
    On Error Resume Next
    
    wsSheet.Close
    Set wsSheet = Nothing
        
    wbExcel.Application.Quit
    Set wbExcel = Nothing
    
    Set rs = Nothing
    
    Exit Function

I do pass wsSheet to some other functions, and that is always done via ByRef wsSheet as Object.

I can't see what's keeping Excel open :banghead: and having it open screws up the app next time the user wants to run this functionality again.

Office 2010 seems a lot worse in this respect than 97 ever was :rolleyes:
 

cable

Access For My Sins
Local time
Today, 12:29
Joined
Mar 11, 2002
Messages
228
After posting this, I removed the on error resume next...it then whinges on the wsSheet.Close statement. Apparently WorkSheets don't support 'Close'!!!

wtf??? Ok it doesn't, according to this: Office Dev Site

But removing that didn't help :(
 

mdlueck

Sr. Application Developer
Local time
Today, 07:29
Joined
Jun 23, 2011
Messages
2,631
I also use late binding to drive Excel. The way I successfully exit Excel is via the following:

Code:
Public Sub SaveExitExcel()
On Error GoTo Err_SaveExitExcel

  'Log the current operation to the StatusBar
  Call SysCmd(acSysCmdSetStatus, "Saving Data and Exiting Excel, Please Wait...")
  DoEvents

  'Save the Workbook
  objExcelApp.ActiveWorkbook.Save

[COLOR=Blue][B]  'Bye bye!
  objExcelApp.Quit
[/B][/COLOR]
Exit_SaveExitExcel:
  Set objExcelWks = Nothing
  Set objExcelWbk = Nothing
  Set objExcelApp = Nothing

  Exit Sub

Err_SaveExitExcel:
  Call errorhandler_MsgBox("Class: " & TypeName(Me) & ", Subroutine: SaveExitExcel()")
  'Disable further error handling, so that the code which is using this object will handle the error
  On Error GoTo 0
  'Raise the error to the caller program
  Err.Raise Number:=vbObjectError + 1032, _
            Source:="Class: " & TypeName(Me) & ", Subroutine: SaveExitExcel()", _
            Description:="Failed to SaveExitExcel()"
  Resume Exit_SaveExitExcel

End Sub
I create three different objects to drive Excel, as follows:

Code:
Public Sub StartExcel()
On Error GoTo Err_StartExcel

  'Log the current operation to the StatusBar
  Call SysCmd(acSysCmdSetStatus, "Starting Excel, Please Wait...")
  DoEvents

  'Get pointer to Excel Object
  Set objExcelApp = CreateObject("Excel.Application")

  'Open the Workbook
  Set objExcelWbk = objExcelApp.Workbooks.Open(Me.BuyerProjectPartsFilename)

  'Attach to the Worksheet
  Set objExcelWks = objExcelWbk.Worksheets("BuyerProjectParts")

  'Activate the Worksheet
  objExcelWks.Activate

  'Make Excel Instance Minimized
  objExcelApp.WindowState = 2 '= wdWindowStateMinimize

  'Make Excel Instance visible
  objExcelApp.Visible = True

Exit_StartExcel:
  Exit Sub

Err_StartExcel:
  Call errorhandler_MsgBox("Class: " & TypeName(Me) & ", Subroutine: StartExcel()")
  'Disable further error handling, so that the code which is using this object will handle the error
  On Error GoTo 0
  'Raise the error to the caller program
  Err.Raise Number:=vbObjectError + 1031, _
            Source:="Class: " & TypeName(Me) & ", Subroutine: StartExcel()", _
            Description:="Failed to StartExcel()"
  Resume Exit_StartExcel

End Sub
 

Users who are viewing this thread

Top Bottom