簡體   English   中英

導航到現有Internet Explorer窗口中的新URL

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

我正在嘗試獲取現有的(打開的)IE窗口,以通過ie.Navigate加載新的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

作為類似/適應的替代方法,這是我最喜歡的功能。

如果存在,則此函數返回一個表示現有 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

更多信息和示例在這里

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM