Navigate to new URL in existing Internet Explorer window

后端 未结 2 839
长发绾君心
长发绾君心 2021-01-14 09:30

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

2条回答
  •  误落风尘
    2021-01-14 10:32

    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
    

提交回复
热议问题