Ok thanks. I wanted to log in using a user name and password... However, I have a further question at the bottom of this post.
Here is the code I found / amended (just in case anyone needs something similar in future)
Sub LogInToSite()
Dim ie As InternetExplorer
Dim myTextField As Variant
Dim IDispatch As Variant
Dim doc As Variant
Set ie = New InternetExplorer
'Set ie = CreateObject("InternetExplorer.Application")
Sheets("results").Activate
With ie
.Visible = False
.Navigate "
Do Until .ReadyState = READYSTATE_COMPLETE: DoEvents: Loop
Set myTextField = .Document.all.Item("tbUserName")
If myTextField Is Nothing = False Then ' We assume we are the login page.
myTextField.Value = "chris"
Set myTextField = .Document.all.Item("tbPassword")
myTextField.Value = "xxxxxxx"
ie.Document.forms(0).btnlogin.Click
End If
'ie.Document.forms(0).submit
'Document.forms(0).btnlogin.Click
Do Until .ReadyState = READYSTATE_COMPLETE: DoEvents: Loop
Do While .Busy: DoEvents: Loop
Set doc = ie.Document
'Exit Sub
GetATable doc, "b3", 18, "PortfolioHighlights"
'Dirty way to get to the main details. Renavigate. Maybe change to cliking on the link instead.
.Navigate2 "
Do Until .ReadyState = READYSTATE_COMPLETE: DoEvents: Loop
Set doc = ie.Document
GetATable doc, "B7", 16, "StuffDetails"
' GetAllTables doc
End With
ie.Quit
End Sub
Sub GetATable(d As Variant, theStartCell As String, tableNo As Integer, rangeName As String)
Dim t, e, c, r, I As Variant
Dim tabno, nextrow As Integer
Dim count As Integer
Dim rng, startCell As Range
Set startCell = Range(theStartCell)
count = 0
tabno = 0
nextrow = 0
For Each e In d.all
count = count + 1
Debug.Print e.nodename
If e.nodename = "TABLE" Then
' Stop
Set t = e
tabno = tabno + 1
If tabno = tableNo Then
Set rng = startCell
' rng.Offset(0, -1) = "Table " & tabno
rng.Select
For Each r In t.Rows
For Each c In r.Cells
rng.Value = c.innerText
Set rng = rng.Offset(0, 1)
I = I + 1
Next c
Set rng = rng.Offset(1, -I)
I = 0
Next r
End If
End If
Next e
'Stop
'resize range
Set rng = startCell
rng.Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
Set rng = Selection
Names.Add rangeName, rng
End Sub
The problem is that the second .navigate I use, the .readystate is already READYSTATE_COMPLETE, which means it doesn't wait for the page to load. Unfortunately .readystate is readonly, so I can't write to it. How do I get around this?
Thanks in advance,
Chris