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

标签 excel vba internet-explorer window

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

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

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

关于excel - 导航到现有 Internet Explorer 窗口中的新 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31167200/

相关文章:

excel - 避免从已声明和设置的 WS 中选择范围时出现运行时错误

python - 将 Python 输出打印到 Excel 工作表

c# - 是什么原因导致错误HRESULT : 0x800A03EC?

excel - 使用 Find 方法时找不到匹配项时出错

c# - 从 C# dll 使用 ADODB 连接到 SQL 服务器

css - 减少列表项宽度以占据 Internet Explorer 11 中文本内容的确切大小

Javascript 在 I.E 中不起作用,但在 FF 中工作正常

vb.net - Excel 引用对数据透视表 RefreshTable 无效

javascript - $.get() 在 IE8 或 IE7 中不起作用

excel - VBA Excel在HTMLObject中获取文本