Consulting

Results 1 to 7 of 7

Thread: Opening multople tabs in IE from Excel and VBA

  1. #1
    VBAX Newbie
    Joined
    Mar 2013
    Posts
    2
    Location

    Opening multople tabs in IE from Excel and VBA

    I have been programming for a few years, but am fairly new to VBA. I am trying to write a script at work and VBA is all we have access to.

    I have an excel sheet with a few thousand links on it and I am writing a script that searches through them for a few in particular and then needs to open four links in a new internet explorer window in separate tabs.

    I have the rest of the program working to find the right 4 links, and then I call this function to open them and this is where it causes problems.

    Function openLinks(url1, url2, url3, url4)
    Set WshShell = CreateObject("WScript.Shell")
    Set ie = CreateObject("InternetExplorer.Application")
    ie.navigate url1
    ie.Navigate2 url2, 2048
    ie.Navigate2 url3, 2048
    ie.Navigate2 url4, 2048
    End Function
    This function seems to work in principle, but it only opens 2 or 3 tabs before giving me this error:

    Run-time error '-2147467259 (80004005)':

    Method 'Navigate2' of object 'IWebBrowser 2' failed
    the strange thing is that it will usually open 2 tabs, but sometimes 3 before throwing an error and very occasionally it will open all 4 without error. I can run it 5 times in a row without changing anything and it is random how many work.

    I searched Google already and followed a suggestion to slow it down between opening tabs by entering this between them.

     While ie.Busy
    DoEvents
    Wend
    And this made no diference at all.

    I then read a suggestion to add this between tabs to slow it down, with varying lengths of time.
     Application.Wait DateAdd("s", 1, Now)
    and this made things worse. it now errored on the very next navigate2 call after the delay. In fact, if I used both, then it would error on the very next call to check ie.busy after a delay too.

    Has anybody come across this kind of issue before? or has any suggestion to get this working? I'm especially confused because of the intermittant nature of the problem.

    Thank you for any help you can offfer.
    Last edited by Aussiebear; 10-04-2022 at 11:58 PM. Reason: Adjusted code tags

  2. #2
    I don't why, but you must force the 2048 flags argument to be passed as a Long data type by appending '&' to it. Also, the IE .Busy Or .ReadyState wait is only needed for the first Navigate because the code must wait until the first tab is opened before opening the new tabs. New tabs opened by the subsequent Navigates are then opened asynchronously - see http://msdn.microsoft.com/en-us/libr...=VS.85%29.aspx - and the same wait has no effect because at that point IE is ready and not busy, so the IE wait isn't needed

    This works for me in IE8:

    Function OpenLinks(URL1 As String, URL2 As String, URL3 As String, URL4 As String)
    
        Dim IE As Object
        
        Set IE = CreateObject("InternetExplorer.Application")
        
        With IE
            .Visible = True
            
            .Navigate URL1, 0
            While .Busy Or .ReadyState <> 4: DoEvents: Wend
            
            .Navigate URL2, 2048&
            .Navigate URL3, 2048&
            .Navigate URL4, 2048&
        End With
        
    End Function
    If you want to specify a variable for the Flags argument, you must declare it as Variant and append & to denote a Long value, like this:
    Function OpenLinks2(URL1 As String, URL2 As String, URL3 As String, URL4 As String)
    
        Dim IE As Object
        Dim flags As Variant
        
        Set IE = CreateObject("InternetExplorer.Application")
        
        With IE
            .Visible = True
            
            flags = 0
            .Navigate URL1, flags
            While .Busy Or .ReadyState <> 4: DoEvents: Wend
            
            flags = 2048&
            .Navigate URL2, flags
            .Navigate URL3, flags
            .Navigate URL4, flags
        End With
        
    End Function

  3. #3
    VBAX Newbie
    Joined
    Mar 2013
    Posts
    2
    Location
    Thank you for your help, I have tried both versions of the function, but I am getting the same problem.

    Now I either get exactly the same error, or sometimes it gives an error on the busy method instead of the navigate2 method

    Method 'Busy' of object 'IWebBrowser 2' failed

    It's the intermittant nature of this issue that is so frustrating.

    Could this be something to do with the network settings I am using? I am doing this at work and the network is pretty locked down, might it be timing out and breaking the connection between Excel and IE so quickly?

  4. #4
    Quote Originally Posted by Griff
    Thank you for your help, I have tried both versions of the function, but I am getting the same problem.

    Now I either get exactly the same error, or sometimes it gives an error on the busy method instead of the navigate2 method

    Method 'Busy' of object 'IWebBrowser 2' failed

    It's the intermittant nature of this issue that is so frustrating.

    Could this be something to do with the network settings I am using? I am doing this at work and the network is pretty locked down, might it be timing out and breaking the connection between Excel and IE so quickly?
    Did you ever find out what the issue was? I am experiencing a very similar issue on the intranet at my work. I received the same error when running my macro, but when stepping through the code, it looks slightly different :

    vba run-time error '-2147467259 (80004005)':
    automation error
    unspecified error

    I try opening 2 windows with 7 tabs each and they both only open anywhere from 2 to 4 tabs each time I try. When stepping through the code, it always errors out on the first attempt to open a URL in a new tab instead of new window (objIE.Navigate2 strURL, 2048).

    Thanks,
    MP
    Last edited by misterpinkey; 06-28-2013 at 02:27 PM.

  5. #5

    Working!

    I just got mine working.

    From Internet Explorer, go to 'Tools', then 'Internet Options'. Select the 'Security' tab, then make sure the checkbox is enabled next to the 'Enable Protected Mode (requires restarting Internet Explorer)' option. Close all Internet Explorer windows, then try your code again.

    I have Windows 7 and am using IE8. My full code is below (I removed my URL's because of the PIP policy my work has, just replace them with yours):

    Sub Passdown_A()
    
        Dim objIE As SHDocVw.InternetExplorer
        Dim strURL(1 To 14) As String
        Dim i as Integer
    
        strURL(1) = "URL #1"
        strURL(2) = "URL #2"
        strURL(3)= "URL #3"
        strURL(4)= "URL #4"
        strURL(5)= "URL #5"
        strURL(6)= "URL #6"
        strURL(7) = "URL #7"
        strURL(8) = "URL #8"
        strURL(9) = "URL #9"
        strURL(10) = "URL #10"
        strURL(11) = "URL #11"
        strURL(12) = "URL #12"
        strURL(13) = "URL #13"
        strURL(14) = "URL #14"
    
        Set objIE = CreateObject("InternetExplorer.Application")
        objIE.Visible = True
        objIE.Navigate2 strURL(1)
        For i = 2 To 7
            objIE.Navigate2 strURL(i), 2048
        Next i
    
        Set objIE = CreateObject("InternetExplorer.Application")
        objIE.Visible = True
        objIE.Navigate2 strURL(8)
        For i = 9 To 14
            objIE.Navigate2 strURL(i), 2048
        Next i
        
        Set objIE = Nothing
    
    End Sub
    Also, I just tried this on another machine at my work. I opened Internet Explorer and checked the 'Enable Protected Mode' checkbox, then closed/opened Internet Explorer and checked again - the box still had a check mark in it. When I tried running the macro, it still only opened a couple tabs in each window. From one of those tabs, I checked the security settings and found that the 'Enable Protected Mode' checkbox was not checked. While still in the security settings, I enabled it again, then closed all IE windows. The next time I ran the macro, it worked and has worked since.

    ~mp
    Last edited by Aussiebear; 10-04-2022 at 11:56 PM. Reason: Adjusted code tags

  6. #6
    VBAX Newbie
    Joined
    Oct 2022
    Posts
    2
    Location

    AUTOMATE ALL THE OPEN LINK

    Quote Originally Posted by misterpinkey View Post
    I just got mine working.

    From Internet Explorer, go to 'Tools', then 'Internet Options'. Select the 'Security' tab, then make sure the checkbox is enabled next to the 'Enable Protected Mode (requires restarting Internet Explorer)' option. Close all Internet Explorer windows, then try your code again.

    I have Windows 7 and am using IE8. My full code is below (I removed my URL's because of the PIP policy my work has, just replace them with yours):

    Sub Passdown_A()
    
        Dim objIE As SHDocVw.InternetExplorer
        Dim strURL(1 To 14) As String
        Dim i as Integer
    
        strURL(1) = "URL #1"
        strURL(2) = "URL #2"
        strURL(3)= "URL #3"
        strURL(4)= "URL #4"
        strURL(5)= "URL #5"
        strURL(6)= "URL #6"
        strURL(7) = "URL #7"
        strURL(8) = "URL #8"
        strURL(9) = "URL #9"
        strURL(10) = "URL #10"
        strURL(11) = "URL #11"
        strURL(12) = "URL #12"
        strURL(13) = "URL #13"
        strURL(14) = "URL #14"
    
        Set objIE = CreateObject("InternetExplorer.Application")
        objIE.Visible = True
        objIE.Navigate2 strURL(1)
        For i = 2 To 7
            objIE.Navigate2 strURL(i), 2048
        Next i
    
        Set objIE = CreateObject("InternetExplorer.Application")
        objIE.Visible = True
        objIE.Navigate2 strURL(8)
        For i = 9 To 14
            objIE.Navigate2 strURL(i), 2048
        Next i
        
        Set objIE = Nothing
    
    End Sub
    Also, I just tried this on another machine at my work. I opened Internet Explorer and checked the 'Enable Protected Mode' checkbox, then closed/opened Internet Explorer and checked again - the box still had a check mark in it. When I tried running the macro, it still only opened a couple tabs in each window. From one of those tabs, I checked the security settings and found that the 'Enable Protected Mode' checkbox was not checked. While still in the security settings, I enabled it again, then closed all IE windows. The next time I ran the macro, it worked and has worked since.

    ~mp

  7. #7
    VBAX Newbie
    Joined
    Oct 2022
    Posts
    2
    Location
    Hi do you have code to populate all the open tab on IE?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •