如何使用FindWindow在VBA中查找具有部分名称的可见或不可见窗口

我正在使用Excel VBA的Windows API使用FindWindow()函数来处理特定窗口,但FindWindow()需要窗口的完整标题/标题来查找。

问题1

P_Win = FindWindow(vbNullString, "PlusApi_Excel Sample_17_39_12 Api Generated Orders")在我的情况下,窗口将更改名称(dynamic)(窗口名称的某些部分将被固定,某些部分将是dynamic的)

防爆。 窗口名称是第一次"PlusApi_Excel Sample_17_39_12 Api Generated Orders" ,第二次将是"PlusApi_Excel Sample_17_45_13 Api Generated Orders"我想我需要打电话窗口与部分名称,但我不知道该怎么办,请亲切地帮助我

问题2

以上的挑战我有一个更多的问题PlusApi将被隐藏,但我的代码仍然显示正面的价值。

我想我只需要调用"visible"窗口。

我在这个vbforums.com的答案find了下面的代码,并增强它寻找可见或不可见的窗口 ,所以希望能回答你的问题:

 Option Explicit Private Declare Function FindWindow Lib "User32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long Private Declare Function GetWindowText Lib "User32" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long Private Declare Function GetWindowTextLength Lib "User32" Alias "GetWindowTextLengthA" (ByVal hWnd As Long) As Long Private Declare Function GetWindow Lib "User32" (ByVal hWnd As Long, ByVal wCmd As Long) As Long Private Declare Function IsWindowVisible Lib "User32" (ByVal hWnd As Long) As Boolean Private Const GW_HWNDNEXT = 2 Private Sub Test() Dim lhWndP As Long If GetHandleFromPartialCaption(lhWndP, "Excel") = True Then If IsWindowVisible(lhWndP) = True Then MsgBox "Found VISIBLE Window Handle: " & lhWndP, vbOKOnly + vbInformation Else MsgBox "Found INVISIBLE Window Handle: " & lhWndP, vbOKOnly + vbInformation End If Else MsgBox "Window 'Excel' not found!", vbOKOnly + vbExclamation End If End Sub Private Function GetHandleFromPartialCaption(ByRef lWnd As Long, ByVal sCaption As String) As Boolean Dim lhWndP As Long Dim sStr As String GetHandleFromPartialCaption = False lhWndP = FindWindow(vbNullString, vbNullString) 'PARENT WINDOW Do While lhWndP <> 0 sStr = String(GetWindowTextLength(lhWndP) + 1, Chr$(0)) GetWindowText lhWndP, sStr, Len(sStr) sStr = Left$(sStr, Len(sStr) - 1) If InStr(1, sStr, sCaption) > 0 Then GetHandleFromPartialCaption = True lWnd = lhWndP Exit Do End If lhWndP = GetWindow(lhWndP, GW_HWNDNEXT) Loop End Function 

代码search一个部分标题为“Excel”的窗口,并告诉你它是否find它,以及它是否是一个可见的窗口。 你应该能够适应你自己的目的。