获取 VBA 中的最后一个活动窗口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16261660/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 20:51:48  来源:igfitidea点击:

Get last active window in VBA

vbawindow

提问by mayur2j

This is not a duplicate question.

这不是一个重复的问题。

I have a UserFormwhich remains on top of all the windows all the time. I want to access source of an already open webpage. I can't use findWindowfunction as I will have multiple browser window open with the exact same title. I am left with only one option - Switch to the required browser window and as the UserFormis on top click some button to switch to the last active window. After that i'll be using getForegroundWindowfunction.

我有一个UserForm一直保留在所有窗户顶部的东西。我想访问一个已经打开的网页的源代码。我无法使用findWindow函数,因为我将打开多个具有完全相同标题的浏览器窗口。我只剩下一个选项 - 切换到所需的浏览器窗口,并UserForm在顶部单击某个按钮以切换到最后一个活动窗口。之后我将使用getForegroundWindow函数。

How can I Switch to the last active window without using findWindowor SendKeys {"%Tab"}(This doesn't work)? I have searched whole internet but couldn't get an answer.

如何在不使用findWindowor 的情况下切换到最后一个活动窗口SendKeys {"%Tab"}(这不起作用)?我已经搜索了整个互联网,但无法得到答案。

Minimizing the form and getting the handle is working but while checking all open windows getting error on Forloop line as No more threads can be created in the system

最小化表单并获取句柄正在工作,但在检查所有打开的窗口时在For循环线上出现错误No more threads can be created in the system

Function GetIEByHWND(myHWND As Long) As InternetExplorer
    Dim tempWindow As Variant
    Dim objShellWindows As New SHDocVw.ShellWindows

    Set GetIEByHWND = Nothing

    On Error GoTo errhandler

    For Each tempWindow In objShellWindows
        If InStr(tempWindow.Path, "INTERNET") Then
           If myHWND = tempWindow.hwnd Then
               Set GetIEByHWND = tempWindow
               Exit For
            End If
        End If
    Next tempWindow

    Exit Function

回答by Siddharth Rout

Is this what you are trying? This is based on THEORY 2

这是你正在尝试的吗?这是基于THEORY 2

Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Declare Function GetForegroundWindow Lib "user32" () As Long

Const SW_MINIMIZE = 6
Const SW_RESTORE = 9

Private Sub UserForm_Initialize()
    Application.Visible = False
End Sub

Private Sub CommandButton1_Click()
    Dim hwnd As Long, RetVal As Long

    hwnd = FindWindow("ThunderDFrame", Me.Caption)
    RetVal = ShowWindow(hwnd, SW_MINIMIZE)

    '~~> This is required so that GetForegroundWindow
    '~~> doesn't pick up the userforms handle
    Sleep 2000

    hwnd = GetForegroundWindow()

    If GetIEByHWND(hwnd) Is Nothing Then
       MsgBox "Not Able to get the object"
    Else
       MsgBox "Was able to get the object"
    End If

    RetVal = ShowWindow(hwnd, SW_RESTORE)

    Application.Visible = True
End Sub

Function GetIEByHWND(myHWND As Long) As Object
    Dim tempWindow As Variant
    Dim objShellWindows As New SHDocVw.ShellWindows

    Set GetIEByHWND = Nothing

    For Each tempWindow In objShellWindows
        If InStr(1, tempWindow.Path, "INTERNET", vbTextCompare) Then
            If myHWND = tempWindow.hwnd Then
                Set GetIEByHWND = tempWindow
                Exit For
            End If
        End If
    Next tempWindow
End Function

FOLLOWUP

跟进

See this code where I am interacting with the IE object.

请参阅我与 IE 对象交互的代码。

Private Sub CommandButton1_Click()
    Dim hwnd As Long, RetVal As Long
    Dim IE As Object

    hwnd = FindWindow("ThunderDFrame", Me.Caption)
    RetVal = ShowWindow(hwnd, SW_MINIMIZE)

    '~~> This is required to that GetForegroundWindow
    '~~> doesn't pick up the userforms handle
    Sleep 2000

    hwnd = GetForegroundWindow()

    Set IE = GetIEByHWND(hwnd)

    If IE Is Nothing Then
       MsgBox "Not Able to get the object"
    Else
       MsgBox "Was able to get the object"

       IE.Visible = False '<~~ Interacting with IE

       Sleep 2000

       IE.Visible = True
    End If

    RetVal = ShowWindow(hwnd, SW_RESTORE)

    Application.Visible = True
End Sub

回答by ozmike

I was getting an error in GetIEbyHWND.

我在 GetIEbyHWND 中遇到错误。

try putting a sleep in between

尝试在两者之间睡一觉

hwnd = GetForegroundWindow()
Sleep 2000
Set IE = GetIEByHWND(hwnd)

Or this solution

或者这个解决方案

Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Declare Function GetForegroundWindow Lib "user32" () As Long

Function oGetLatestIE(sURL As String, & _
Optional sWindowTitle As String = "MyWindowtitle") As Object

Dim IE As InternetExplorer
Set IE = New InternetExplorer

IE.Navigate sURL

Dim hwnd As Long

hwnd = GetForegroundWindow() ' get latest IE window
Set IE = Nothing  ' work around for windows 7 (intranet)

i = 0
Do While i < 10 And IE Is Nothing

    i = i + 1
    Set objShellApp = CreateObject("Shell.Application")
    For Each objWindow In objShellApp.Windows
        DoEvents

        If LCase(objWindow.LocationName) = LCase(sWindowTitle) Then

            If objWindow.hwnd = hwnd Then 'active IE window if more than one with same title
            Set IE = objWindow

            End If
        End If
    Next
    DoEvents
    sleep 100
Loop

If IE Is Nothing Then
MsgBox "nothing"
Exit Function
End If

Set oGetLatestIE = IE

End Function