Set Focus to Internet Explorer Object in Visual Basic

后端 未结 4 922
别跟我提以往
别跟我提以往 2020-12-11 21:13

Does anybody know how to set focus onto an IE object with Visual Basic? I\'ve tried myieobject.SetFocus, but the compiler errors with this statement.

4条回答
  •  轮回少年
    2020-12-11 22:03

    Try this. Open Internet explorer by shell command , where you can define focus(this is focused and small windows) and after that catch that shell/explorer window and define it as Internet explorer object. Maybe there is better way than sleep to wait.

    Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
    
    Sub call_IE()
    Dim IE As InternetExplorer
    Dim htmldoc As HTMLDocument
    
    Set IE = Open_Focused_explorer()
    IE.Navigate "google.com"
    Set htmldoc = IE.Document
    
    End Sub
    
    Function Open_Focused_explorer() As InternetExplorer
    Dim shellWins As ShellWindows
    
    'if windows are 64bit IE is on diferent location
    #If Win64 Then
         Shell "C:\Program Files (x86)\Internet Explorer\iexplore.exe", vbNormalFocus
    #Else
        Shell "C:\Program Files\Internet Explorer\iexplore.exe", vbNormalFocus
    #End If
    
    
     'wait until explorer is full loaded
     Sleep 4000
    
     On Error Resume Next
        'create collection of all explorers
      Set shellWins = New ShellWindows
    
        If shellWins.Count > 0 Then
            ' Get last one
            Set Open_Focused_explorer = shellWins.Item(shellWins.Count - 1)
        End If
    On Error GoTo 0
    
    End Function
    

提交回复
热议问题