câu hỏi đơn giản nhưng để giải quyết quả chẳng dể tí nào!Từ Excel, tôi dùng VBA tạo 1 code để mở một file .exe bằng lệnh SHELL.
Xin các bạn cho tôi biết: Muốn đóng lại file .exe đó, tôi dùng lệnh nào trong VBA.
Cảm ơn các bạn
Private Declare Function CloseHandle Lib "Kernel32.dll" (ByVal Handle As Long) As Long
Private Declare Function OpenProcess Lib "Kernel32.dll" (ByVal dwDesiredAccessas As Long, ByVal bInheritHandle As Long, ByVal dwProcId As Long) As Long
Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
Private Declare Function CreateToolhelpSnapshot Lib "kernel32" Alias "CreateToolhelp32Snapshot" (ByVal lFlags As Long, lProcessID As Long) As Long
Private Declare Function ProcessFirst Lib "kernel32" Alias "Process32First" (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long
Private Declare Function ProcessNext Lib "kernel32" Alias "Process32Next" (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long
Private Type PROCESSENTRY32
dwSize As Long
cntUsage As Long
th32ProcessID As Long
th32DefaultHeapID As Long
th32ModuleID As Long
cntThreads As Long
th32ParentProcessID As Long
pcPriClassBase As Long
dwFlags As Long
szExeFile As String * 260
End Type
Private Sub KillProcess(myName As String)
Dim uProcess As PROCESSENTRY32
Dim rProcessFound As Long
Dim hSnapshot As Long
Dim szExename As String
Dim exitCode As Long
Dim myProcess As Long
Dim AppKill As Boolean
Dim appCount As Long
Dim i As Long
On Local Error GoTo Finish
appCount = 0
uProcess.dwSize = Len(uProcess)
hSnapshot = CreateToolhelpSnapshot(2&, 0&)
rProcessFound = ProcessFirst(hSnapshot, uProcess)
Do While rProcessFound
i = InStr(1, uProcess.szExeFile, Chr(0))
szExename = LCase$(Left$(uProcess.szExeFile, i - 1))
If Right$(szExename, Len(myName)) = LCase$(myName) Then
appCount = appCount + 1
myProcess = OpenProcess(1&, -1&, uProcess.th32ProcessID)
AppKill = TerminateProcess(myProcess, 0&)
Call CloseHandle(myProcess)
End If
rProcessFound = ProcessNext(hSnapshot, uProcess)
Loop
Call CloseHandle(hSnapshot)
Finish:
End Sub
Sub Main()
KillProcess "Test.exe"
End Sub
Sub Close_Process()
Dim sCloseProcess As String
'Thay process Bác muốn tại đây. Ví dụ: Excel.exe
sCloseProcess = "TASKKILL /F /IM [COLOR="Red"]Excel.exe[/COLOR]"
Shell sCloseProcess , vbHide
End Sub
Bác thử dùng code này:
Mã:Sub Close_Process() Dim sCloseProcess As String 'Thay process Bác muốn tại đây. Ví dụ: Excel.exe sCloseProcess = "TASKKILL /F /IM [COLOR="Red"]Excel.exe[/COLOR]" Shell sCloseProcess , vbHide End Sub
Lê Văn Duyệt