vba 在 Visual Basic 中将焦点设置为 Internet Explorer 对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11196968/
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-08 13:23:31  来源:igfitidea点击:

Set Focus to Internet Explorer Object in Visual Basic

internet-explorervbasetfocus

提问by Lloyd Banks

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.

有人知道如何使用 Visual Basic 将焦点设置到 IE 对象上吗?我已经试过了myieobject.SetFocus,但是这个语句的编译器错误。

采纳答案by Mike Lewis

I needed a spreadsheet of mine to "set focus" to Internet Explorer after performing a function so I didn't have to bother clicking on it. This is what I found to work:

我需要一个我的电子表格来在执行一个功能后“设置焦点”到 Internet Explorer,这样我就不必费心点击它了。这就是我发现的工作:

      Const myPageTitle As String = "Title of my webpage"
      Const myPageURL As String = "http://www.mywebpage.com"
      Dim myIE As SHDocVw.InternetExplorer
      Dim myIE As InternetExplorer
      Set myIE = GetOpenIEByTitle(myPageTitle, False)


      myIE.visible = false
      DoEvents
      myIE.visible = true  
     'for some reason, making the page invisible then visible always ensures it pops up

    Function GetOpenIEByTitle(i_Title As String, _
                          Optional ByVal i_ExactMatch As Boolean = True) As SHDocVw.InternetExplorer
Dim objShellWindows As New SHDocVw.ShellWindows

  If i_ExactMatch = False Then i_Title = "*" & i_Title & "*"
  'ignore errors when accessing the document property
  On Error Resume Next
  'loop over all Shell-Windows
  For Each GetOpenIEByTitle In objShellWindows
    'if the document is of type HTMLDocument, it is an IE window
    If TypeName(GetOpenIEByTitle.document) = "HTMLDocument" Then
      'check the title
      If GetOpenIEByTitle.document.Title Like i_Title Then
        'leave, we found the right window
        Exit Function
      End If
    End If
  Next
End Function

回答by Eddie Pelostontos

Try this:

尝试这个:

'First, hide the object even if it's visible
myieobject.Visible = False
' Second, show the object to focusing
myieobject.Visible = True

回答by SeanC

set .Visible=True- if you've lost the screen somewhere in popups, you would have to loop through window titles to activate a specific title.

设置.Visible=True- 如果您在弹出窗口的某处丢失了屏幕,则必须循环浏览窗口标题以激活特定标题。

Dim objShell As Shell
Dim objIndex As InternetExplorer

Set objShell = New Shell

For Each objIndex In objShell.Windows
    If TypeName(objIndex.Document) = "HTMLDocument" Then
        If InStr(objIndex.Document.Title, "Stack Overflow") > 0 Then
            objIndex.Visible = True
            Exit For
        End If
    End If
Next objIndex

Here's what you cando with the IE object: MSDN

以下是您可以对 IE 对象执行的操作:MSDN

回答by Lynx

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.

尝试这个。通过 shell 命令打开 Internet Explorer,您可以在其中定义焦点(这是焦点和小窗口),然后捕获该 shell/explorer 窗口并将其定义为 Internet Explorer 对象。也许有比睡觉更好的等待方式。

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