【问题标题】:Navigate to new URL in existing Internet Explorer window导航到现有 Internet Explorer 窗口中的新 URL
【发布时间】:2015-07-01 16:45:46
【问题描述】:

我正在尝试通过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

【问题讨论】:

  • IE.navigate "http://www.google.com"
  • 感谢蒂姆,这有助于解决这部分问题,但现在它突出显示 getIE = True,我熟悉 PHP 并从那里调用函数,但我是否在 VBA 中正确调用?跨度>
  • 你不能给函数赋值——我不清楚你对那行的意思。

标签: excel vba internet-explorer window


【解决方案1】:
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

【讨论】:

  • 谢谢蒂姆,接近了,但我想要的是让现有打开的 IE 窗口转到新的 URL yahoo.com,我从其他讨论中发现这是我在上面发布的代码函数。
  • 这正是我运行它时所做的。
【解决方案2】:

作为类似/改编的替代方案,这是我最喜欢的功能。

此函数返回一个表示现有 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

更多信息和示例here

【讨论】:

  • (显然,您需要在您的机器上安装 Internet Explorer 才能使用此功能)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-25
  • 1970-01-01
  • 1970-01-01
  • 2013-08-28
  • 2011-10-28
相关资源
最近更新 更多