【问题标题】:Why does, within Task Manager, the Excel process not exist when I close Form1 but the Outlook process does?为什么在任务管理器中,当我关闭 Form1 时 Excel 进程不存在,而 Outlook 进程却存在?
【发布时间】:2017-04-06 21:25:53
【问题描述】:
  1. 运行以下代码并检查您的任务管理器以查看 Excel 和 Outlook 进程是否存在:

    Imports Microsoft.Office.Interop
    
    Public Class Form1
    
        Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
    
            'Kill all EXCEL.EXE's from Task Manager
            For Each prog As Process In Process.GetProcessesByName("EXCEL")
                prog.Kill()
            Next
    
            'Kill all OUTLOOK.EXE's from Task Manager
            For Each prog As Process In Process.GetProcessesByName("OUTLOOK")
                prog.Kill()
            Next
    
            'Open new EXCEL.EXE in the Task Manager
            Dim xlApp As New Excel.Application
    
            'Open new OUTLOOK.EXE in the Task Manager
            Dim olApp As New Outlook.Application
    
        End Sub
    
    End Class
    
  2. 关闭Form1

  3. 检查您的任务管理器,发现 Excel 进程不存在,但 Outlook 进程存在。

为什么在任务管理器中,当我关闭 Form1 时 Excel 进程不存在,而 Outlook 进程却存在?

【问题讨论】:

    标签: vb.net excel-interop


    【解决方案1】:

    我无法重现您遇到的这个问题。 Outlook 进程确实需要更长的时间才能结束,但它确实结束了。

    话虽如此,有更好的方法可以关闭这些进程。正如我在评论中所说,看看这个answer 链接到Siddharth Rout's 位code。

    首先,您需要在类级别声明 xlApp 和 olApp。您还需要在代码中添加以下方法:

    Private Sub ReleaseObject(ByVal obj As Object)
        Try
            Dim intRel As Integer = 0
            Do
                intRel = System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
            Loop While intRel > 0
            obj = Nothing
        Catch ex As Exception
            obj = Nothing
        Finally
            GC.Collect()
        End Try
    End Sub
    

    然后您可以在关闭表单时调用此方法:

    Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
        xlApp.Quit()
        ReleaseObject(xlApp)
    
        olApp.Quit()
        ReleaseObject(olApp)
    End Sub
    

    总体而言,您的代码将类似于以下内容:

    Public Class Form1
    
        Private xlApp As Excel.Application
        Private olApp As Outlook.Application
    
        Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
    
            'Open new EXCEL.EXE in the Task Manager
            xlApp = New Excel.Application
    
            'Open new OUTLOOK.EXE in the Task Manager
            olApp = New Outlook.Application
    
        End Sub
    
        Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
            xlApp.Quit()
            ReleaseObject(xlApp)
    
            olApp.Quit()
            ReleaseObject(olApp)
        End Sub
    
        Private Sub ReleaseObject(ByVal obj As Object)
            Try
                Dim intRel As Integer = 0
                Do
                    intRel = System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
                Loop While intRel > 0
                obj = Nothing
            Catch ex As Exception
                obj = Nothing
            Finally
                GC.Collect()
            End Try
        End Sub
    
    End Class
    

    【讨论】:

      【解决方案2】:

      尝试像这样启动 Excel:

      Dim xlApp As New Process
      xlApp.StartInfo.FileName = "filePath/excel.exe"
      xlApp.Start
      

      如果这没有帮助,试试这个来启动一个 exe:

      Process.Start("filePath\excel.exe")
      

      【讨论】:

        猜你喜欢
        • 2018-01-24
        • 1970-01-01
        • 1970-01-01
        • 2014-12-30
        • 1970-01-01
        • 1970-01-01
        • 2012-05-05
        • 1970-01-01
        • 2011-06-25
        相关资源
        最近更新 更多