vba and FindWindowLike

AMAS

Active Member
Joined
Apr 11, 2010
Messages
472
Hi,

I can't seem to get the hang of using the windows API function FindWindowLike. I'm trying to use it instead of the exact window title. Does anyone have any experience or thoughts on what I am doing wrong here:

Code:
Option Explicit
Dim r

Sub test()
  Static hWnds() As Variant
  Dim hWnd
    hWnd = FindWindowLike(hWnds(), 0, "NotePad*", "*", Null)
    Debug.Print hWnd
End Sub

Function FindWindowLike(hWndArray() As Variant, ByVal hWndStart As Variant, WindowText As String, Classname As String, ID) As Integer
    r = FindWindowLike(hWndArray(), hWndStart, WindowText, Classname, ID)
End Function

AMAS
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Is that all the code you have?
 
Upvote 0
Hi Norie,

I'm still learning to use API. I was able to use ShowWindow but am trying to making more flexible.

Code:
Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdSHow As Long) As Long  
Function apicShowWindow(strClassName As String, strWindowName As String, lngState As Long) 
  'Get window handle. 
  Dim lngWnd As Long 
  Dim intRet As Integer  
  lngWnd = FindWindow(strClassName, strWindowName) 
  apicShowWindow = ShowWindow(lngWnd, lngState) 
End Function
 
Upvote 0
The original code I used came from techrepublic.com. I modified the code from tip #11 to activate and display the NotePad window (works fine). Now I want to replace the FindWindow call with a FindWindowLike call so that I don't need to know the exact window title to manipulate it. I did attempt to use a snippet from the the Daily Dose of Excel but no dice. Can you help me replace the FindWindowLike function instead of the FindWindow function?

Code:
Option Explicit

Private Declare PtrSafe Function ShowWindow Lib "user32" _
                (ByVal hwnd As Long, ByVal nCmdSHow As Long) As Long
Private Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" _
                (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Sub API_Window()
    Call apicShowWindow("Notepad", "Untitled - Notepad", 1)
End Sub

Function apicShowWindow(strClassName As String, strWindowName As String, lngState As Long)
  Dim lngWnd&, intRet%
  ' Use Findwindow and ShowWindow together to manipulate window
    lngWnd = FindWindow(strClassName, strWindowName)
    apicShowWindow = ShowWindow(lngWnd, lngState)
End Function
 
Upvote 0
If I was going to replace the FindWindow function with a FindWindowLike I would use the code in the link I posted.

You said you only used a 'snippet' of that code, why?

Did you try it with all the code instead of just a 'snippet'?
 
Upvote 0
Hi Norie,

OK, I went back and adjusted the code using the template provided by Rob van Gelder on the Daily Dose of Excel blog. I realized I was missing some functions and made some adjustments to make it work with my sample. In the end it works but is a lot longer and slower than I had anticipated. I guess the important thing is that it works. Thanks for your help.

AMAS

Code:
Option Explicit
Declare Function GetDesktopWindow Lib "user32" () As Long
Declare Function GetWindow Lib "user32" (ByVal hWnd As Long, ByVal wCmd As Long) As Long
Declare Function ShowWindow Lib "user32" (ByVal hWnd As Long, ByVal nCmdSHow As Long) As Long
Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

Sub API_Window()
  Dim ClassName$, SearchName$, WinName$
    ClassName = "Notepad"
    SearchName = "Notepad"
    WinName = FindWindowLike(GetDesktopWindow(), SearchName, ClassName)
    Call apicShowWindow(ClassName, WinName, 3)
End Sub

Function FindWindowLike(hWndParent As Long, Caption As String, ClassName As String) As String
  Dim hWnd&
  Const GW_HWNDNEXT = 2, GW_CHILD = 5
  ' Find window using a like function
    hWnd = GetWindow(hWndParent, GW_CHILD)
    Do Until hWnd = 0
        If WindowText(hWnd) Like "*" & Caption & "*" Then
            FindWindowLike = WindowText(hWnd)
            Exit Do
        End If
        hWnd = GetWindow(hWnd, GW_HWNDNEXT)
    Loop
End Function

Function WindowText(hWnd As Long) As String
  Dim lng&, str$
  Const WM_GETTEXT = &HD, WM_GETTEXTLENGTH = &HE
  ' Convert ID to text
    If hWnd <> 0 Then
        lng = SendMessage(hWnd, WM_GETTEXTLENGTH, 0&, 0&) + 1
        If lng > 0 Then
            str = String$(lng, vbNullChar)
            lng = SendMessage(hWnd, WM_GETTEXT, lng, ByVal str)
            If lng > 0 Then WindowText = Left$(str, lng)
        End If
    End If
End Function

Function apicShowWindow(strClassName As String, strWindowName As String, lngState As Long)
  Dim lngWnd&, intRet%
  ' Use Findwindow and ShowWindow together to manipulate window
    lngWnd = FindWindow(strClassName, strWindowName)
    apicShowWindow = ShowWindow(lngWnd, lngState)
End Function
 
Upvote 0

Forum statistics

Threads
1,213,527
Messages
6,114,140
Members
448,551
Latest member
Sienna de Souza

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