This was a tricky one to find this but got there in the end.
Basically I wanted Excel/VBA to take a look and see if a PDF file was currently open and if so give me the document title. Code can be easily modified to look for anything. Original code modified from VB from http://www.freevbcode.com/ShowCode.Asp?ID=920
Note that the code below only looks for the first match then backs out - check the VB code above for the structure to continually recurse through until all matches are found.
Private Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As Any, _
ByVal lpWindowName As Any) As Long
Private Declare Function GetWindow Lib "user32.dll" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Private Declare Function GetWindowText Lib "user32.dll" Alias "GetWindowTextA" (ByVal hwnd As Long, _
ByVal lpString As String, ByVal nMaxCount As Long) As Long
Private Declare Function GetParent Lib "user32.dll" (ByVal hwnd As Long) As Long
Dim hwndapp As Long
Dim nret As Long
Dim hwndtmp As Long
Dim titletmp As String
gw_hwndnext = 2
titlepart = "WHAT YOU ARE LOOKING FOR OR LEAVE BLANK MAYBE? I WAS GOING FOR PDF"
titlepart = UCase(titlepart)
hwndtmp = FindWindow(0&, 0&)
Do Until hwndtmp = 0
If GetParent(hwndtmp) = 0 Then
titletmp = Space(256)
nret = GetWindowText(hwndtmp, titletmp, Len(titletmp))
If nret Then
titletmp = UCase(Left(titletmp, nret))
If InStr(titletmp, titlepart) Then
Exit Do
End If
End If
End If
hwndtmp = GetWindow(hwndtmp, gw_hwndnext)
Loop
Title = titletmp ' so title gives you the app/window title you were looking for
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment