Image: Kindly read the image from top left to right while coming down
I’m trying to import data (a cell value) from IE to Excel given there is a lag to generate the table.
Here’s what I’m trying to do via excel:
- Go to FOIS site. (Done)
- Input “Description” from excel. (Done)
- Input “Unit Details” from excel. (Done)
- Get cell value: [Freight_Rate][Train_Load]. (Not Done)
- Given 20X10 table with headers [Station_From][Station_To], fill it with respective Step 4 value. (Not Done)
Despite a lot of tries, for some the cell value fails to populate into excel.
Any help would be great. Thank you.
Here’s the code that I wrote:
Following is for the first 3 steps:
Sub ChromAuto()
Dim IE As Object
Dim doc As HTMLDocument
Dim table As HTMLTable
Dim rateclass As String
Dim fgtrate As String
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.navigate "https://www.fois.indianrail.gov.in/FOISWebPortal/pages/FWP_FrgtCalcNew.jsp"
Do While IE.Busy
Application.wait DateAdd("s", 1, Now)
Loop
Set doc = IE.document
doc.getElementById("txtSttnFrom").Value = ThisWorkbook.Sheets("Data").Range("C2").Value
doc.getElementById("txtSttnTo").Value = ThisWorkbook.Sheets("Data").Range("C3").Value
doc.getElementById("txtWgonType").Value = ThisWorkbook.Sheets("Data").Range("C4").Value
doc.getElementById("txtCtgy").Value = ThisWorkbook.Sheets("Data").Range("C6").Value
doc.getElementById("txtCmdtName").Value = ThisWorkbook.Sheets("Data").Range("C7").Value
If ThisWorkbook.Sheets("Data").Range("C5").Value = "General" Then
doc.getElementById("selDvsn").Value = "GA"
ElseIf ThisWorkbook.Sheets("Data").Range("C5").Value = "Low Rated (A-Division)" Then
doc.getElementById("selDvsn").Value = "LA"
ElseIf ThisWorkbook.Sheets("Data").Range("C5").Value = "Low Rated (B-Division)" Then
doc.getElementById("selDvsn").Value = "LB"
ElseIf ThisWorkbook.Sheets("Data").Range("C5").Value = "Low Rated (C-Division)" Then
doc.getElementById("selDvsn").Value = "LC"
ElseIf ThisWorkbook.Sheets("Data").Range("C5").Value = "Low Rated (D-Division)" Then
doc.getElementById("selDvsn").Value = "LD"
End If
doc.getElementById("cmdGetInfo").Click
Do While IE.Busy Or IE.readyState <> 4
Application.wait DateAdd("s", 1, Now)
Loop
If ThisWorkbook.Sheets("Data").Range("C9").Value = "Rake" Then
doc.getElementById("selRKPM").Value = "R"
ElseIf ThisWorkbook.Sheets("Data").Range("C9").Value = "Mini Rake" Then
doc.getElementById("selRKPM").Value = "M"
ElseIf ThisWorkbook.Sheets("Data").Range("C9").Value = "Piecemeal" Then
doc.getElementById("selRKPM").Value = "P"
End If
doc.getElementById("txtWgonNumb").Value = 20
IE.Quit
Set IE = Nothing
End Sub
Following is the try for step 4 where I’m stuck:
'Wait for the table to be available
Do While doc.getElementById("tblFrgtCalcDtls") Is Nothing
Application.wait DateAdd("s", 1, Now)
Loop
Application.wait DateAdd("s", 3, Now)
' Retrieve the table
' Set table = doc.getElementById("tblFrgtCalcDtls")
' Set table = doc.getElementById("tblFrgtCalcDtls")
' Set table = doc.getElementsByClassName("col col-lg-6 col-md-6 col-sm-12 col-xs-12")(0)
'Try 2
Dim HTMLTables As MSHTML.IHTMLElementCollection
Dim HTMLTable As MSHTML.IHTMLElement
Dim HTMLDiv As MSHTML.IHTMLElement
Dim TableSection As MSHTML.IHTMLElement
Set HTMLDiv = doc.getElementById("dataDiv")
Set HTMLTables = HTMLDiv.getElementsByTagName("table")
For Each HTMLTable In HTMLTables
Debug.Print HTMLTable.ID, HTMLTable.className
For Each TableSection In HTMLTable.Children
Debug.Print , TableSection.Tag
Next TableSection
Next HTMLTable
' Debug.Print table
' If Not table Is Nothing Then
' Extract cell values
' rateclass = table.Rows(1).Cells(2).innerText
' fgtrate = table.Rows(7).Cells(2).innerText
' Print the values
' ThisWorkbook.Sheets("Data").Range("C11").Value = rateclass
' ThisWorkbook.Sheets("Data").Range("C12").Value = fgtrate
' Else
' Debug.Print "Error: Table not found on the webpage."
' End If
' rateclass = doc.getElementById("lblAdd").innerText
AIBot is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.