Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VBA - Get Taskbar Applications

Tags:

excel

vba

winapi

I found a 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 out the name and description of all active processes.

Example:

OUTLOOK.EXE

EXCEL.EXE

However, there is another tab in Task manager that displays applications (which are the same found in 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 out:

Microsoft Excel - Book1

Inbox - [email protected]

VBA - Get Taskbar Applications (<- Chrome)

like image 948
docjay Avatar asked Mar 29 '26 21:03

docjay


1 Answers

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

Update, added my edited version of original authors code to answer 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
like image 105
mrbungle Avatar answered Apr 02 '26 05:04

mrbungle