How to Import application list from Task Manager to Excel?
Well, this can be done with the help of the Windows API Function from the library Kernel32. Using this we will be able to view the processes currently executing in our system and we don’t have to open the task manager every time.
In this article we will be using just three of those plenty of Functions available in Windows and we are going to fetch the task list from Task Manager through VBA Macro. If it is possible to fetch the process list, then it is also possible to automate the task monitoring tasks and we can tell the code what to do when any particular task runs. We can choose to give high priority to a task or hide it or terminate it. Thus, you are taking first step in controlling the system with VBA through the basics explained in this page.
How to use Task Manager – Read and Monitor Task List?
Why do we need to Import this task list from Task Manager?
- Automate Task Monitoring: To monitor the running processes in Windows, we cannot keep on watching the Task Manager just like watching TV. Alternatively we could fetch the Task List and Process the list according to our need.
- Analyze: Sometimes we may be in need to analyze whether any Spy Program, Keylogger or suspicious application is running in our system.
- Task Scheduling Software: Using the code in this page and Timer in excel code a advanced Task managing system with scheduling option can be developed.
These are a few of the practical usage of the sample code in this article. Now, these details can be fetched using the below WINAPI Functions.
1. CreateToolhelp32Snapshot Lib "kernel32" (ByVal lFlags As Long, ByVal lProcessID As Long) As Long 2. Process32First Lib "kernel32" (ByVal hSnapshot As Long, sPE32 As PROCESSENTRY32) As Long 3. Process32Next Lib "kernel32" (ByVal hSnapshot As Long, sPE32 As PROCESSENTRY32) As Long
Also Read: Export Attachments from Outlook Email to Local Folder.
Access Task Manager from Excel
Before we code the function, we have to declare some special Variables and API Functions in the beginning of the code.
- Open a new Excel workbook.
- Press Alt + F11 to view VB Editor.
- Copy paste the below code, then Press F5 to execute it.
This will fetch the Processes displayed in the task manager to sheet1.
'API Functions and Variables Declaration
Private Const INVALID_HANDLE_VALUE = -1&
Private Const TH32CS_SNAPPROCESS = &H2
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 * 1000 'Increase this limit if there are more than 1000 Process
End Type
Private Declare Sub CloseHandle Lib "kernel32" (ByVal hPass As Long)
Private Declare Function CreateToolhelp32Snapshot Lib "kernel32" (ByVal lFlags As Long, ByVal lProcessID As Long) As Long
Private Declare Function Process32First Lib "kernel32" (ByVal hSnapshot As Long, PE32 As PROCESSENTRY32) As Long
Private Declare Function Process32Next Lib "kernel32" (ByVal hSnapshot As Long, PE32 As PROCESSENTRY32) As Long
'Function to Fetch Process List
Private Sub Task_Manager_Process_List_To_Sheet()
'Variables Declaration
Dim PE32 As PROCESSENTRY32
Dim Proc_Name As String
Dim hSnapshot As Long
Dim iRow As Integer
Dim iCol As Integer
Dim iRet1 As Integer
Dim lRet As Long
'Get Snapshop of All Process.
'To get details about Threads of each process, refer MSDN for additonal Parameters
hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0&)
If hSnapshot <> INVALID_HANDLE_VALUE Then
PE32.dwSize = Len(PE32)
lRet = Process32First(hSnapshot, PE32)
iRow = 2
iCol = 1
'Fetch Each Process Details one by one
Do While lRet
iRet1 = InStr(1, PE32.szExeFile, VBA.Strings.Chr(0))
If iRet1 > 0 Then
Proc_Name = VBA.Strings.Left(PE32.szExeFile, iRet1 - 1)
ThisWorkbook.Sheets(1).Cells(iRow, iCol).Value = Proc_Name
ThisWorkbook.Sheets(1).Cells(iRow, iCol + 1).Value = PE32.th32ProcessID
iRow = iRow + 1
End If
lRet = Process32Next(hSnapshot, PE32)
Loop
CloseHandle hSnapshot
End If
End Sub
The above code fetch only the processes that are visible in Task Manager. To view the hidden processes and threads started with these process, please refer the MSDN link mentioned at the beginning of the article.
More Tricks: Import Email Details from Outlook to Excel through VBA
External Reference: MSDN Library – WINAPI Functions used in VBA to get Task list from Task Manager.