简体   繁体   English

导航到现有Internet Explorer窗口中的新URL

[英]Navigate to new URL in existing Internet Explorer window

I am trying to get an existing (open) IE window to load a new URL via ie.Navigate . 我正在尝试获取现有的(打开的)IE窗口,以通过ie.Navigate加载新的URL。

Below is my worksheet code which loads a website on click of a button: 下面是我的工作表代码,单击按钮即可加载网站:

Private Sub Sendit_Click()
    Dim IE As Object

    Set IE = CreateObject("InternetExplorer.Application")
    IE.Visible = True
    IE.navigate "https://www.yahoo.com"
    Do
    Loop Until IE.ReadyState = READYSTATE_COMPLETE

    GetIE

    Err_Clear:
    If Err <> 0 Then
        Err.Clear
        Resume Next
    End If
End Sub

This code is in my module: 这段代码在我的模块中:

Function GetIE()
    Dim shellWins As ShellWindows
    Dim IE As InternetExplorer

    Set shellWins = New ShellWindows

    If shellWins.Count > 0 Then
        ' Get IE
        Set IE = shellWins.Item(0)
    Else
        ' Create IE
        Set IE = New InternetExplorer
        IE.Visible = True
    End If

    IE.navigate "http://www.google.com"

    Set shellWins = Nothing
    Set IE = Nothing
End Function

IE doesn't navigate to http://www.google.com . IE不会导航到http://www.google.com

Sub TestGetIE()

    Dim IE As Object
    Set IE = CreateObject("Internetexplorer.Application")
    IE.Visible = True
    IE.navigate "https://www.yahoo.com"
    WaitFor IE

    Set IE = GetIE("https://www.yahoo.com")

    IE.navigate "http://www.google.com"


End Sub

Sub WaitFor(IE)
    Do
    Loop Until IE.readyState = READYSTATE_COMPLETE
End Sub


Function GetIE(sLocation As String) As Object

Dim objShell As Object, objShellWindows As Object, o As Object
Dim sURL As String
Dim retVal As Object

    Set retVal = Nothing
    Set objShell = CreateObject("Shell.Application")
    Set objShellWindows = objShell.Windows

    For Each o In objShellWindows
        sURL = ""
        'check the URL and if it's the one you want then
        ' assign it to the return value
        sURL = o.LocationURL
        If sURL Like sLocation & "*" Then
            Set retVal = o
            Exit For
        End If
    Next o

    Set GetIE = retVal

End Function

As a similar/adapted alternative, here's a favorite function of mine. 作为类似/适应的替代方法,这是我最喜欢的功能。

This function returns an object representing the existing InternetExplorer instance if there is one, otherwise a newly-created instance. 如果存在,则此函数返回一个表示现有 InternetExplorer实例的对象,否则返回一个新创建的实例。

Function GetIE() As Object
'return an object for the open Internet Explorer window, or create new one
  For Each GetIE In CreateObject("Shell.Application").Windows() 'Loop to find
    If (Not GetIE Is Nothing) And GetIE.Name = "Internet Explorer" Then Exit For 'Found!
  Next GetIE
  If GetIE Is Nothing Then Set GetIE=CreateObject("InternetExplorer.Application")'Create
  GetIE.Visible = True 'Make IE window visible
End Function

More information and example here . 更多信息和示例在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM