在任务管理器vb.net中终止上次创建的Excel实例

在任务管理器vb.net中终止上次创建的Excel实例,excel,vb.net,Excel,Vb.net,你能帮我解决我的问题吗 我打开了几个Excel实例(task manager中的Excel.EXE),我想杀死最后一个打开的实例。现在我可以先杀一个,但我想杀最后一个: Sub Main() Dim dictExe As Object dictExe = CreateObject("Scripting.Dictionary") Dim oServ As Object Dim cProc Dim oProc As Object oServ = Ge

你能帮我解决我的问题吗

我打开了几个Excel实例(task manager中的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
我想在字典中删除创建日期最大的实例,但我不知道如何删除。
所以问题也可能是,如何从字典中获取具有最大值的键?

我将放弃使用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
Sub-Main()
将oServ作为对象
暗cProc
作为对象的Dim oProc
Dim oToBeRemovedProc作为对象
Dim oToBeRemovedCreationDate作为对象
oServ=GetObject(“winmgmts:”)
cProc=oServ.ExecQuery(“从Win32_进程中选择*)
对于cProc中的每个oProc
如果oProc.Name=“EXCEL.EXE”,则
“:::没有很好的对照检查值
如果oToBeRemovedProc不算什么,那么
oToBeRemovedProc=oProc
oToBeRemovedCreationDate=oProc.CreationDate
如果结束
'::当前保留值是否比此值旧?
如果oToBeRemovedCreationDate
此脚本代码是否在Catia应用程序中?或者您正在实际的VB.NET中工作?这是一个清除例程,用于删除应用程序创建的Excel实例吗?此代码取自Catia VBA并放入VB.NET。我需要关闭Catia应用程序,但我是为excel编写的,因为越来越多的人使用excel。是的,我的应用程序正在创建excel/Catia实例。