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.
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
75,361
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
 

Forum statistics

Threads
1,081,441
Messages
5,358,709
Members
400,508
Latest member
fish31

Some videos you may like

This Week's Hot Topics

  • VBA (Userform)
    Hi All, I just would like to know why my code isn't working. Here is my VBA code: [CODE=vba]Private Sub OKButton_Click() Dim i As Integer...
  • List box that changes fill color
    Hello, I have gone through so many pages trying to figure this out. I have a 2020 calendar that depending on the day needs to have a certain...
  • Remove duplicates and retain one. Cross-linked cases
    Hi all I ran out of google keywords to use and still couldn't find a reference how to achieve the results of a single count. It would be great if...
  • VBA Copy and Paste With Duplicates
    Hello All, I'm in need of some input. My VBA skills are sub-par at best. I've assembled this code from basic research and it works but is...
  • Macro
    is it possible for a macro to run if the active cell value is different to the value above it
  • IF DATE and TIME
    I currently use this to check if date has passed but i also need to set a time on it too. Is it possible? [CODE=vba]=IF(B:B>TODAY(),"Not...
Top