VBA automation of Excel leaves a process in memory after Quit

后端 未结 5 2059
一向
一向 2020-12-17 23:59

I have seen a lot of suggestions for this problem, and I have tried them all, but none seem to work. The VBA code is in a non-Microsoft product (SAP Business Objects, which

相关标签:
5条回答
  • 2020-12-18 00:07

    Adding an answer based on David Zemens comment. Works for me.

    m_oExcel.Quit            '<- Still in Task Manager after this line
    Set m_oExcel = Nothing   '<- Gone after this line
    
    0 讨论(0)
  • 2020-12-18 00:16

    Though this isn't supposed to happen, you could send excel a "WindowClose" message in order to force close.

    You'll be needing these API functions

    Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
    Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
    Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
    Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hWnd As Long, lpdwProcessId As Long) As Long
    Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    

    And it should look something like this:

    // First, get the handle
    hWindow = FindWindow(vbNullString, "Excel")
    //Get proccess ID
    GetWindowThreadProcessId(hWindow, ProcessValueID)
    //Kill the process
    ProcessValue = OpenProcess(PROCESS_ALL_ACCESS, CLng(0), ProcessValueID)
    TerminateProcess(ProcessValue, CLng(0))
    CloseHandle ProcessValueID
    
    0 讨论(0)
  • 2020-12-18 00:17

    Most times this happens because Excel is keeping a COM Add-in open. Try using the link below for help on removing the COM Add-in.

    Add or remove add-ins

    I find particular comfort in the note:

    Note This removes the add-in from memory but keeps its name in the list of available add-ins. It does not delete the add-in from your computer.

    0 讨论(0)
  • 2020-12-18 00:23

    This question has already been answered by Acantud in response to a subsequent post: https://stackoverflow.com/questions/25147242 Fully qualify your references to objects within the Excel workbook you open to avoid creating orphaned processes in the task manager. In this case, the solution is to prefix DelimitedList with m_oBook, such as

    ThisDocument.Variables("Agent(s)").Value = m_oBook.DelimitedList("Agents")
    
    0 讨论(0)
  • 2020-12-18 00:32

    Need to use only:

    Private Sub Workbook_BeforeClose(Cancel As Boolean)
    
        Excel.Application.Quit
    
    End Sub
    
    0 讨论(0)
提交回复
热议问题