4

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

4

4 回答 4

5

在执行一个功能后,我需要一个我的电子表格来“设置焦点”到 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
于 2014-06-26T02:05:20.317 回答
3

试试这个:

'First, hide the object even if it's visible
myieobject.Visible = False
' Second, show the object to focusing
myieobject.Visible = True
于 2019-12-30T12:31:33.833 回答
1

设置.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

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

于 2012-06-25T21:40:00.240 回答
0

试试这个。通过 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
于 2018-02-27T15:05:38.517 回答