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.Visual Basic1231. CreateToolhelp32Snapshot Lib "kernel32" (ByVal lFlags As Long, ByVal lProcessID As Long) As Long2. Process32First Lib "kernel32" (ByVal hSnapshot As Long, sPE32 As PROCESSENTRY32) As Long3. Process32Next Lib "kernel32" (ByVal hSnapshot As Long, sPE32 As PROCESSENTRY32) As Long
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.Visual Basic1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556'API Functions and Variables DeclarationPrivate Const INVALID_HANDLE_VALUE = -1&Private Const TH32CS_SNAPPROCESS = &H2Private Type PROCESSENTRY32dwSize As LongcntUsage As Longth32ProcessID As Longth32DefaultHeapID As Longth32ModuleID As LongcntThreads As Longth32ParentProcessID As LongpcPriClassBase As LongdwFlags As LongszExeFile As String * 1000 'Increase this limit if there are more than 1000 ProcessEnd TypePrivate Declare Sub CloseHandle Lib "kernel32" (ByVal hPass As Long)Private Declare Function CreateToolhelp32Snapshot Lib "kernel32" (ByVal lFlags As Long, ByVal lProcessID As Long) As LongPrivate Declare Function Process32First Lib "kernel32" (ByVal hSnapshot As Long, PE32 As PROCESSENTRY32) As LongPrivate Declare Function Process32Next Lib "kernel32" (ByVal hSnapshot As Long, PE32 As PROCESSENTRY32) As Long'Function to Fetch Process ListPrivate Sub Task_Manager_Process_List_To_Sheet()'Variables DeclarationDim PE32 As PROCESSENTRY32Dim Proc_Name As StringDim hSnapshot As LongDim iRow As IntegerDim iCol As IntegerDim iRet1 As IntegerDim lRet As Long'Get Snapshop of All Process.'To get details about Threads of each process, refer MSDN for additonal ParametershSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0&)If hSnapshot <> INVALID_HANDLE_VALUE ThenPE32.dwSize = Len(PE32)lRet = Process32First(hSnapshot, PE32)iRow = 2iCol = 1'Fetch Each Process Details one by oneDo While lRetiRet1 = InStr(1, PE32.szExeFile, VBA.Strings.Chr(0))If iRet1 > 0 ThenProc_Name = VBA.Strings.Left(PE32.szExeFile, iRet1 - 1)ThisWorkbook.Sheets(1).Cells(iRow, iCol).Value = Proc_NameThisWorkbook.Sheets(1).Cells(iRow, iCol + 1).Value = PE32.th32ProcessIDiRow = iRow + 1End IflRet = Process32Next(hSnapshot, PE32)LoopCloseHandle hSnapshotEnd IfEnd 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.
External Reference: MSDN Library – WINAPI Functions used in VBA to get Task list from Task Manager.
Reading Process List From Task Manager