导航到现有Internet Explorer窗口中的新URL [英] Navigate to new URL in existing Internet Explorer window

查看:105
本文介绍了导航到现有Internet Explorer窗口中的新URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取一个现有的(打开的)IE窗口,以通过ie.Navigate加载新的URL.

I am trying to get an existing (open) IE window to load a new URL via ie.Navigate.

下面是我的工作表代码,只需单击一个按钮即可加载网站:

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

此代码在我的模块中:

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无法导航到 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

这篇关于导航到现有Internet Explorer窗口中的新URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆