VBA - Get Taskbar Applications

I found the code online:

Public Sub showProcesses()
    Dim W As Object
    Dim ProcessQuery As String
    Dim processes As Object
    Dim process As Object
    Set W = GetObject("winmgmts:")
    ProcessQuery = "SELECT * FROM win32_process"
    Set processes = W.execquery(ProcessQuery)
    For Each process In processes
        MsgBox process.Name
        MsgBox process.Description
    Next
    Set W = Nothing
    Set processes = Nothing
    Set process = Nothing
End Sub

      

It prints the name and description of all active processes.

Example:

OUTLOOK.EXE

EXCEL.EXE

However, there is another tab in the Task Manager that displays the applications (which are the same on the taskbar). I want to create a program that reads their names.

Example:

In my taskbar, Chrome, Outlook and Excel are open applications, so I want my program to print:

Microsoft Excel - Book1

Inbox - Somerandomemail@thisisnotreal.yzbbr

VBA - Get Taskbar Applications (<- Chrome)

+3


source to share


1 answer


This should point in the right direction. I was able to test this and see the results in the Immediate window (ctrl-G). You will need to edit to display in cells. http://access.mvps.org/access/api/api0013.htm

Update, added my edited version of the authors source code to answer the question



Private Declare PtrSafe Function apiGetClassName Lib "user32" Alias _
                "GetClassNameA" (ByVal Hwnd As Long, _
                ByVal lpClassname As String, _
                ByVal nMaxCount As Long) As Long
Private Declare PtrSafe Function apiGetDesktopWindow Lib "user32" Alias _
                "GetDesktopWindow" () As Long
Private Declare PtrSafe Function apiGetWindow Lib "user32" Alias _
                "GetWindow" (ByVal Hwnd As Long, _
                ByVal wCmd As Long) As Long
Private Declare PtrSafe Function apiGetWindowLong Lib "user32" Alias _
                "GetWindowLongA" (ByVal Hwnd As Long, ByVal _
                nIndex As Long) As Long
Private Declare PtrSafe Function apiGetWindowText Lib "user32" Alias _
                "GetWindowTextA" (ByVal Hwnd As Long, ByVal _
                lpString As String, ByVal aint As Long) As Long
Private Const mcGWCHILD = 5
Private Const mcGWHWNDNEXT = 2
Private Const mcGWLSTYLE = (-16)
Private Const mcWSVISIBLE = &H10000000
Private Const mconMAXLEN = 255

Function fEnumWindows()
Dim lngx As Long, lngLen As Long
Dim lngStyle As Long, strCaption As String

    lngx = apiGetDesktopWindow()
    'Return the first child to Desktop
    lngx = apiGetWindow(lngx, mcGWCHILD)

    Do While Not lngx = 0
        strCaption = fGetCaption(lngx)
        If Len(strCaption) > 0 Then
            lngStyle = apiGetWindowLong(lngx, mcGWLSTYLE)
            'enum visible windows only
            If lngStyle And mcWSVISIBLE Then
                 ActiveCell.Value = fGetCaption(lngx)
                 ActiveCell.Offset(1, 0).Activate
            End If
        End If
        lngx = apiGetWindow(lngx, mcGWHWNDNEXT)
    Loop
End Function


Private Function fGetCaption(Hwnd As Long) As String
    Dim strBuffer As String
    Dim intCount As Integer

    strBuffer = String$(mconMAXLEN - 1, 0)
    intCount = apiGetWindowText(Hwnd, strBuffer, mconMAXLEN)
    If intCount > 0 Then
        fGetCaption = Left$(strBuffer, intCount)
    End If
End Function


Sub test()
Range("A1").Activate
 Call fEnumWindows
End Sub

      

+6


source







All Articles