【问题标题】:Kill last created Excel instance in task manager vb.net在任务管理器 vb.net 中杀死最后创建的 Excel 实例
【发布时间】:2017-11-09 18:19:04
【问题描述】:

你能帮我解决我的问题吗?

我打开了几个 Excel 实例(任务管理器中的 EXCEL.EXE),我想杀死最后打开的实例。现在我可以杀死第一个实例,但我想杀死最后一个:

Sub Main()
    Dim dictExe As Object
    dictExe = CreateObject("Scripting.Dictionary")

    Dim oServ As Object
    Dim cProc
    Dim oProc As Object
    oServ = GetObject("winmgmts:")
    cProc = oServ.ExecQuery("Select * from Win32_Process")
    For Each oProc In cProc
        If oProc.Name = "EXCEL.EXE" Then
            dictExe.Add(oProc, oProc.CreationDate)
        End If
    Next

    For Each oProcCatia In dictExe
        If oProcCatia.Name = "EXCEL.EXE" Then
            Dim errReturnCode
            errReturnCode = oProcCatia.Terminate()
            Exit Sub
        End If
    Next
End Sub

我想在字典中杀死实例,女巫有最大的创建日期,但我不知道怎么做。 那么问题也可以是,如何从字典中获取最大值的键?

【问题讨论】:

  • 此脚本代码是否在 Catia 应用程序中?还是您在实际的 VB.NET 中工作?
  • 这是删除应用程序创建的 Excel 实例的清理例程吗?
  • 此代码取自 Catia VBA 并放入 vb.net。我需要关闭 Catia 应用程序,但我写的是 excel,因为更多人使用 excel。问题是一样的。
  • 是的,我的应用程序正在创建 excel/Catia 实例。

标签: excel vb.net


【解决方案1】:

我会放弃使用 Scripting.Dictionary 并简单地保留最新 CreationDate 的参考:

Sub Main()
    Dim oServ As Object
    Dim cProc
    Dim oProc As Object
    Dim oToBeRemovedProc As Object
    Dim oToBeRemovedCreationDate As Object

    oServ = GetObject("winmgmts:")
    cProc = oServ.ExecQuery("Select * from Win32_Process")
    For Each oProc In cProc
        If oProc.Name = "EXCEL.EXE" Then
            '::: No good checking values against Nothing
            If oToBeRemovedProc Is Nothing Then
                oToBeRemovedProc = oProc
                oToBeRemovedCreationDate = oProc.CreationDate
            End If

            '::: Is the current hold value older than this one?
            If oToBeRemovedCreationDate < oProc.CreationDate Then
                oToBeRemovedProc = oProc
                oToBeRemovedCreationDate = oProc.CreationDate
            End If
        End If
    Next

    '::: Do we actually have something to remove?
    If Not oToBeRemovedProc Is Nothing Then
        oToBeRemovedProc.Terminate()
        oToBeRemovedProc = Nothing
        oToBeRemovedCreationDate = Nothing 
    End If
End Sub

【讨论】:

    猜你喜欢
    • 2017-07-10
    • 1970-01-01
    • 2019-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-17
    • 1970-01-01
    相关资源
    最近更新 更多