1

これは重複した質問ではありません。

私はUserForm常にすべてのウィンドウの上にある を持っています。既に開いている Web ページのソースにアクセスしたい。findWindowまったく同じタイトルで複数のブラウザウィンドウを開くため、機能を使用できません。残された選択肢は 1 つだけです。必要なブラウザ ウィンドウに切り替え、UserForm一番上にあるボタンをクリックして、最後にアクティブだったウィンドウに切り替えます。その後、getForegroundWindow関数を使用します。

orを使用せずに最後のアクティブなウィンドウに切り替えるにはどうすればよいですfindWindowSendKeys {"%Tab"}(これは機能しません)。インターネット全体を検索しましたが、答えが得られませんでした。

フォームを最小化してハンドルを取得することは機能していますが、開いているすべてのウィンドウをチェックしているときに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
4

2 に答える 2

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

ファローアップ

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
于 2013-04-28T13:43:01.050 に答える
0

GetIEbyHWND でエラーが発生しました。

間に睡眠を入れてみてください

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

または、このソリューション

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
于 2013-12-10T00:14:48.490 に答える