我需要识别当前正在运行的 Excel 进程 ID,以便将其与其他 Excel 进程 ID 进行比较,以便删除其他非当前 Excel 进程。这是我所拥有的,但没有一个进程 ID = 当前 ID。我究竟做错了什么?
Declare Function GetCurrentProcessId Lib "kernel32" () As Int32
Private Sub CheckMemory()
Dim process As Process
Dim xlProcessCount = process.GetProcessesByName("EXCEL").Count()
Dim curProc As IntPtr = GetCurrentProcessId()
Dim currentProcess As Process = process.GetProcessById(curProc)
If xlProcessCount > 1 Then
' MsgBox("There are " + xlProcessCount + "Excel processes running", MsgBoxStyle.Information)
'Associate the current active "Excel.exe" process with this application instance
'and delete the other running processes
Dim current = process.GetCurrentProcess()
For Each process In process.GetProcessesByName("EXCEL")
If process.Id <> currentProcess.Id Then
MsgBox("Deleting a previous running Excel process", MsgBoxStyle.Information)
process.Kill()
' releaseObject(xlApp)
' releaseObject(xlWb)
' releaseObject(xlWs)
End If
Next
End If