Navigate2 Debugging

1.4k views Asked by At

I'm attempting to create a macro through VBA to open a specified set of links in multiple tabs on IE. Currently I'm using the code below, and it works most of the time if I am trying to open 3 or less tabs. Anything more than 3, the code crashes at the "Navigate2" section. After some research, I cannot seem to find a common issue or a resolution for this. The code appears similar to below (links have been removed for obvious reasons).

Any help would be greatly appreciated.

Sub USD_ILB()
  Dim strURL As String
  Dim file_date As String
  Dim objIE As Object
  Dim arrSites(4)

  file_date = Format(Cells(1, 2), "dd.mm.yyyy")

  arrSites(0) = "URL1"
  arrSites(1) = "URL2"
  arrSites(2) = "URL3"
  arrSites(3) = "URL4"
  arrSites(4) = "URL5"
  Set objIE = CreateObject("InternetExplorer.Application")
  For i = 0 To 4 Step 1
     strURL = arrSites(i)
     If i = 0 Then
         objIE.Navigate strURL
     Else
         objIE.Navigate2 strURL, 2048
     End If
  Next i
  objIE.Visible = True
  Set objIE = Nothing

End Sub
2

There are 2 answers

1
Keith Brown On

As regards the 2048 in objIE.Navigate2 strURL, 2048

Extracted from Microsoft Developer Network, MSHTML Reference, IWebBrowser2

object.Navigate2(URL, Flags, TargetFrameName, PostData, Headers)

Flags [in, optional]
.. a combination of the values defined by the BrowserNavConstants enumeration

Enum BrowserNavConstants:  ...  navOpenInNewTab = 2048 ...
1
ChipsLetten On

You could try adding a check whether IE is busy.

For i = 0 To 4 Step 1
    Do While objIE.Busy
        DoEvents
    Loop
    strURL = arrSites(i)
    If i = 0 Then
        objIE.Navigate strURL
    Else
        objIE.Navigate2 strURL, 2048
    End If
Next i