How to get the Parent application name of active window?

Premanshu

Board Regular
Joined
Oct 2, 2007
Messages
91
Hi,

I have a code (below) which can extract the name of the window which is currently active on my desktop:-

Code:

Declare Function GetForegroundWindow Lib "user32.dll" () As Long
Declare Function GetWindowTextLength Lib "user32.dll" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
Declare Function GetWindowText Lib "user32.dll" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long

sub Macro1

Dim hforewnd As Long
Dim slength As Long
Dim wintext As String
Dim retval As Long

hforewnd = GetForegroundWindow()
slength = GetWindowTextLength(hforewnd) + 1
wintext = Space(slength)
retval = GetWindowText(hforewnd, wintext, slength)
wintext = Left(wintext, slength - 1)

msgbox wintext

end sub

code:

Can anyone please suggest me how can i get the name of the parent application of the active window. As of now it's only giving the name of the active window.

Please help me with this..... many thanks in advance.

Regards,
Premanshu.
 

Some videos you may like

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
75,559
Office Version
365
Platform
Windows
You could try using the GetParent API function.

But I've a feeling you might not get the result(s) you want.:)
 

Andrew Poulsom

MrExcel MVP
Joined
Jul 21, 2002
Messages
73,092
Try:

Code:
Declare Function GetForegroundWindow Lib "user32.dll" () As Long
Declare Function GetWindowTextLength Lib "user32.dll" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
Declare Function GetWindowText Lib "user32.dll" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Declare Function GetWindowModuleFileName Lib "user32.dll" Alias "GetWindowModuleFileNameA" (ByVal hwnd As Long, ByVal pszFileName As String, ByVal cchFileNameMax As Long) As Long
 
Sub Macro1()
    Dim hforewnd As Long
    Dim slength As Long
    Dim wintext As String
    Dim retval As Long
    Dim parent As String
 
    hforewnd = GetForegroundWindow()
    slength = GetWindowTextLength(hforewnd) + 1
    wintext = Space(slength)
    retval = GetWindowText(hforewnd, wintext, slength)
    wintext = Left(wintext, slength - 1)
    MsgBox wintext
 
    parent = Space$(255)
    GetWindowModuleFileName hforewnd, parent, Len(parent)
    MsgBox parent
End Sub
 

Watch MrExcel Video

Forum statistics

Threads
1,099,486
Messages
5,468,885
Members
406,618
Latest member
SAMEERS

This Week's Hot Topics

Top