VBA循环已经打开的IE并找到带有TITLE或URL的IE,然后选择它

时间:2016-11-24 16:56:26

标签: excel-vba vba excel

我尝试了以下脚本,但仍然没有运气。

我要求VBA循环已经打开的IE并找到带有TITLE或URL的IE,然后选择它。 (让IE弹出)?

marker = 0

Set objShell = CreateObject("Shell.Application")

IE_count = objShell.Windows.Count

For x = 0 To (IE_count - 1)

    my_url = objShell.Windows(x).Document.Location
    my_title = objShell.Windows(x).Document.Title

    If my_title Like "Hillgate" & "*" Then 'compare to find if the desired web page is already open

        Set IE = objShell.Windows(x)

        marker = 1
        Exit For
    Else
    End If
Next

If marker = 0 Then
    MsgBox ("A matching webpage was found")
Else
    MsgBox ("A matching webpage was not found")
End If

IE.Visible = True

2 个答案:

答案 0 :(得分:0)

我前段时间找到了这段代码并完成了您需要的操作,只需从主函数中调用它:

Sub Example()
Dim IE As Object
  Set IE = GetIeByTitle("Title in WebPage")
  If Not(IE Is Nothing) Then
  '... my stuff
End Sub

' ZVI:2013-03-19 Get IE object of already open window by its title
' Arguments:
'   Title     -  title of the searchied IE window
'   [IsLike]  -  False/True = exact/partial searching, default is False
'   [IsFocus] -  False/True = don't_activate/activate IE window, default is False
Function GetIeByTitle(Title, Optional IsLike As Boolean, Optional IsFocus As Boolean) As Object
  Dim w As Object
  For Each w In CreateObject("Shell.Application").Windows
    With w
      If .Name = "Windows Internet Explorer" Then
        If IsLike Then

          If InStr(1, .locationName & " - " & .Name, Title, vbTextCompare) > 0 Then

            ' Partial title of window is found - activate IE window
            If IsFocus Then
              w.Visible = False
              w.Visible = True
            End If
            Set GetIeByTitle = w
            Exit For
          End If
        Else
          If StrComp(.locationName & " - " & .Name, Title) = 0 Then
            ' Title of window is found - activate IE window
            If IsFocus Then
              w.Visible = False
              w.Visible = True
            End If
            Set GetIeByTitle = w
            Exit For
          End If
        End If
      End If
    End With
  Next
  Set w = Nothing
End Function

答案 1 :(得分:0)

尝试使用此功能 放在子程序的顶部

私有声明函数BringWindowToTop Lib“ user32”(ByVal hwnd as Long)只要

在子例程中放置此

BringWindowToTop IE.hwnd