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.
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
You could try using the GetParent API function.

But I've a feeling you might not get the result(s) you want.:)
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,214,911
Messages
6,122,196
Members
449,072
Latest member
DW Draft

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top