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.
Code:
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:
Quote:
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.
Code:
While ie.Busy
DoEvents
Wend
And this made no difference at all.
I then read a suggestion to add this between tabs to slow it down, with varying lengths of time.
Code:
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 intermittent nature of the problem.
Thank you for any help you can offer.
AUTOMATE ALL THE OPEN LINK
Quote:
Originally Posted by
misterpinkey
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):
Code:
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