I have code to open a site and search for a specific gene (examples for testing mentioned: AXl, TREM2).
If I run the code multiple times and/or change the gene I am searching for, it crashes at:
brow.document.forms("searchForm").elements("query").Value = geneN
It seems the navigate function gives a readystate while not ready.
I put a wait of 10 sec but it seems to skip a correct navigate.
Sub openprot()
Dim brow As New SHDocVw.InternetExplorer
Dim URLp As String
Dim geneN As String 'name of gene
Dim HTMLco As HTMLDocument
Dim allrefs As MSHTML.IHTMLElementCollection
Dim elink As MSHTML.IHTMLElement
Dim lol As String
geneN = Application.InputBox("name your gene", "gene") 'try TREM2 or AXL
URLp = "https://www.proteinatlas.org/"
brow.navigate URLp
Do Until brow.READYSTATE = 4: DoEvents: Loop 'variation tested
'Do While brow.READYSTATE <> READYSTATE_COMPLETE: DoEvents: Loop
'Do While brow.Busy: DoEvents: Loop
'Do Until brow.READYSTATE = READYSTATE_COMPLETE: DoEvents: Loop
brow.document.forms("searchForm").elements("query").Value = geneN
brow.document.forms("searchForm").elements("searchButton").Click
Set HTMLco = brow.document
Application.Wait Now + TimeValue("00:00:02")
Set allrefs = HTMLco.getElementsByTagName("a")
For Each elink In allrefs
lol = elink.href
If InStr(1, lol, geneN, 1) > 0 And InStr(1, lol, "tissue", 1) > 0 Then
elink.Click
Application.Wait Now + TimeValue("00:00:02")
brow.Visible = True
Exit For
End If
Next elink
End Sub