VBA code to open IE and switch to new tab

cb366374

Board Regular
Joined
Feb 25, 2012
Messages
95
I am stuck...obviously. I was tasked with gathering some data on a long list of companies for my supervisor (I'm a grad assistant) from a messy website (i.e. no specific URLs for each report). I wrote a "simple" program to open internet explorer, navigate through a disclaimer page, change the value of a drop-down, wait for the page to automatically update, then click a link that launches a report. My plan was to simply copy the page that is launched and paste it into a worksheet and find the relevant data I need after that. The website turned out to be a nightmare but I managed to launch the report I had specified. However, the report is launched in a new tab and I have no idea how to navigate there.

From what I have read I believe I need to re-write the code using a shell extension but I have no experience with this and was hoping someone could help. I could probably figure it out eventually by teaching myself some of this new programming but it seems like a large task to complete before Monday (spreadsheet is due). Also, this will be a one-time assignment so I'm looking for something as simple as possible and all the other examples seem too complicated. My current code is below. I have hacked it to pieces trying to figure this out so my apologies for messy code. The shell is my main issue but if someone also has a suggestion gathering the information from the final report more efficiently, I would love suggestions. Thanks in advance.

FYI...
Windows 7, Excel 2010, Internet Explorer 9

Code:
Sub gather()
'
    Dim ie As Object
    Dim ElementCol As Object
    Dim btnInput As Object
    Dim linkElement As Object
    Dim Link As Object
    Dim wait As Long
    Dim i As Integer
    Dim compCode As String
    Set ie = CreateObject("internetexplorer.application")
    
    ie.Visible = True
    ie.navigate "https://i-site.naic.org/grid/gridDisc.jsp"
    
    While ie.Busy
        DoEvents
    Wend
    
    Set ElementCol = ie.document.getElementsByTagName("input")
 
    'loop through all 'input' elements and find the one with the value "I Agree"
    For Each btnInput In ElementCol
        If btnInput.Value = "I Agree" Then
            btnInput.Click
            Exit For
        End If
    Next btnInput
    
    While ie.Busy
        DoEvents
    Wend
    
    ie.document.getElementById("Company").Value = "60994"
    
    While ie.Busy
        DoEvents
    Wend
    
    ie.document.forms("criteria").Item("COMPANY").FireEvent ("onchange")
    
    Application.wait Time + TimeSerial(0, 0, 10)
    
    While ie.Busy
        DoEvents
    Wend
    
    ' click a text link on the page after that
    Set linkElement = ie.document.getElementsByTagName("a")
 
    For Each Link In linkElement
        If Link.href = "javascript:fnSubmit()" Then
            Link.Click
            Exit For
        End If
    Next Link
    
    While ie.Busy
        DoEvents
    Wend
        
    ie.ExecWB 17, 0 'SelectAll
    ie.ExecWB 12, 2 'Copy selection
    
    Sheets("Output").Select
    Cells.ClearContents
    Range("A1").Select
    ActiveSheet.Paste
    
End Sub
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.

Forum statistics

Threads
1,223,237
Messages
6,170,928
Members
452,366
Latest member
TePunaBloke

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top