Excel 如何使用FindWindow在VBA中查找具有部分名称的可见或不可见窗口
我正在使用Windows API和Excel VBA来处理特定的窗口,使用Excel 如何使用FindWindow在VBA中查找具有部分名称的可见或不可见窗口,excel,vba,Excel,Vba,我正在使用Windows API和Excel VBA来处理特定的窗口,使用FindWindow()函数,但是FindWindow()需要查找窗口的完整标题/标题 问题1 p_Win=FindWindow(vbNullString,“PlusApi\u Excel示例\u 17\u 39\u 12 Api生成的订单”) 在我的例子中,窗口将更改名称(动态)(窗口名称的某些部分将是固定的,而某些部分将是动态的) 例如,窗口名称为首次“PlusApi\u Excel示例\u 17\u 39\u 12
FindWindow()
函数,但是FindWindow()
需要查找窗口的完整标题/标题
问题1
p_Win=FindWindow(vbNullString,“PlusApi\u Excel示例\u 17\u 39\u 12 Api生成的订单”)
在我的例子中,窗口将更改名称(动态)(窗口名称的某些部分将是固定的,而某些部分将是动态的)
例如,窗口名称为首次“PlusApi\u Excel示例\u 17\u 39\u 12 Api生成订单”
第二次是“PlusApi\u Excel示例\u 17\u 45\u 13 Api生成订单”
我想我需要打电话给窗口的部分名称,但我不知道如何做,请帮助我
问题2
上面的挑战我还有一个问题,PlusApi将被隐藏,但我的代码仍然显示一个正值
我想我只需要调用
“visible”
窗口。我在中找到了以下代码,并对其进行了增强,以查找可见或不可见的窗口,因此希望能回答您的两个问题:
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
选项显式
私有声明函数findwindowlib“User32”别名“FindWindowA”(ByVal lpClassName作为字符串,ByVal lpWindowName作为字符串),长度为
私有声明函数GetWindowText Lib“User32”别名“GetWindowTextA”(ByVal hWnd为长,ByVal lpString为字符串,ByVal cch为长)为长
私有声明函数GetWindowTextLength Lib“User32”别名“GetWindowTextLengthA”(ByVal hWnd As Long)为Long
私有声明函数GetWindow Lib“User32”(ByVal hWnd为Long,ByVal wCmd为Long)为Long
私有声明函数IsWindowVisible Lib“User32”(ByVal hWnd长度)为布尔值
私人建筑GW_HWNDNEXT=2
专用子测试()
暗lhWndP为长
如果GetHandleFromPartialCaption(lhWndP,“Excel”)=True,则
如果IsWindowVisible(lhWndP)=True,则
MsgBox“找到可见窗口句柄:”&lhWndP,vbOKOnly+vbInformation
其他的
MsgBox“找到不可见窗口句柄:”&lhWndP,vbOKOnly+vbInformation
如果结束
其他的
MsgBox“未找到“Excel”窗口!”,vbOKOnly+VBEQUOTION
如果结束
端接头
私有函数GetHandleFromPartialCaption(ByRef lWnd为Long,ByVal sCaption为String)为布尔值
暗lhWndP为长
作为字符串的Dim sStr
GetHandleFromPartialCaption=False
lhWndP=FindWindow(vbNullString,vbNullString)'父窗口
当lhWndP为0时执行此操作
sStr=字符串(GetWindowTextLength(lhWndP)+1,Chr$(0))
GetWindowText lhWndP、sStr、Len(sStr)
sStr=左$(sStr,Len(sStr)-1)
如果仪表(1,sStr,sCaption)>0,则
GetHandleFromPartialCaption=True
lWnd=lhWndP
退出Do
如果结束
lhWndP=GetWindow(lhWndP,GW\U HWNDNEXT)
环
端函数
代码搜索部分标题为“Excel”的窗口,并告诉您是否找到它以及它是否是可见窗口。您应该能够根据自己的目的调整它。它区分大小写,但是可以通过将
GetHandle…
函数中的InStr
行更改为:如果InStr(1,sStr,sCaption,vbTextCompare)>0,那么在使用它之前,是否需要添加对Excel文件中某些内容的引用?(即工具>参考资料)@TekiusFanatikus:无需添加任何特殊参考资料,只需一本标准空白工作簿即可使用。