Part 2 of Main code.
This coding in this post should be copied diretly under the coding from the last post. Together they form a single routine, the Main routine
(The routine, Public Sub GetElemsText( ) , which is posted in the next post is also required for the Main routine to work )
Code:Rem 3a) Directly ' ' ' Simple text file print out using just result of PageSrc from '2a Debug.Print PageSrc ' unfortunately you will unlikely be able to view the whole String as it appears too big. Also pasting to a cell will not make it all visible. However if after pasting the .value from the cell is put in a string and that used in place of Pagesrc in the creation of the DOM it does work, so indicating that the data is there, but just not possible for us to "see". Dim strTextFile As String: Let strTextFile = ThisWorkbook.Path & "\Updates\strTextFile.txt" Dim HghWyNo2 As Long: Let HghWyNo2 = FreeFile(RangeNumber:=1) Open strTextFile For Binary As #HghWyNo2 Put #HghWyNo2, 1, PageSrc ' Use Put to write the whole array at once http://www.vb-helper.com/howto_read_write_binary_file.html https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/put-statement Close HghWyNo2 ' ' ' 'Application.Cursor = xlDefault' Restore the cursor to normal. Rem 3b) Large Object from main made OOP type model object, (HTMLdoc) ( Rem 3b)(i) ) ' Dim Head As Object 'Dim Head As IHTMLElementCollection 'requires Early Binding. getElementsBy___ returns a NodeList which is an interface to the DispHTMLElementCollection which is an internal class that you're not supposed to see/use. It does implement the IHTMLElementCollection though so you can use that. Dim Head As Object ' Unusually this Large main Object is Dim ed as an Object, ..as you find you cannot Dim it as what its TypeName( ) returns ( or as displayed in the Watch Window ), “DispHTMLElementCollection“ . Set Head = HTMLdoc.getElementsByTagName("Table") 'This Object is a massive thing again with loads in, but this time it would appear to be the things "tagged" with < table > < /table > which look like the headings of each table I am interested in Rem 4)(Rem 3b)(ii)) Often we would loop here for each "Table" but in our example we only have one 'Dim oTable As THMLTable ' If we had Early binding, then this would work, because omehow Head has been recognised as a table oTable as HTMLTable.JPG : https://imgur.com/R309JjC , and for ..._ Dim oTable As Object ' _... this table we have typically present in the object ' HTML TableRow count , "column" Count for final Table will need to be calculated, "HTML Cell" count in Entire Table Dim C As Long, r As Long 'Indicies for getting appropriate Row and HTMLTableCell 'Dim n As Long ' Not needed if only one table so only "1 Loop" '4b)=== main working would be Outer loop for each Table in many similar routines==============Building Array from HTML Table 'For n = 0 To Head.Length - 1 ' We only have one table so don't need to loop. The word Length in HTML things is often similar to what Count is in many VBA objects Set oTable = Head(0) ' Somehow Head has been recognised as a table oTable as HTMLTable.JPG : https://imgur.com/R309JjC '4b(i) Fill variable for dimensions variable for each, one on our case, Main loop Dim rowCnt As Long: Let rowCnt = oTable.Rows.Length ' "length" / number of rows in this table Dim colCt As Long: Let colCt = oTable.Cells.Length 'In this object the cells "length" would appear to be the number / count of cells in the entire table Dim colCnt As Long: Let colCnt = Application.WorksheetFunction.RoundUp((colCt / oTable.Rows.Length), 0) ' 'This rounds up to the nearest avarage row width, that is to say column number in a row ' I thought this did ? colCt \ oTable.Rows.Length Dim Data() As String 'Array with string element used for output table. Fixed (static) String type for Text. ReDim Data(0 To rowCnt - 1, 0 To colCnt - 1) 'Output Array, reDimed to table being looked at. ( Hopefully always same column number, might want to hard Code to rowCnt, 11 columns . Because I am using "base" of indicie to start at 0 then I go from 0 to one less than the Count(Length) '4b(ii) Looping through rows to build output array-----------| '---Inner loop does at each row, .... For r = 0 To rowCnt - 1 'Going along the HTML Table rows exactly as pike ' https://www.mrexcel.com/forum/excel-questions/367030-copy-table-website-into-excel-vba.html#post4026613 '--- .... 'go through each Cell( "column" ) in that row. For C = 0 To colCnt - 1 'Going along the HTML Table Cells (columns) exactly the same as pike '4b(ii)a Build Output Array Call GetElemsText(oTable.Rows(r).Cells(C), Data(r, C)) 'Data(r, c) = oTable.Rows(r).Cells(c).innerText ' pike, kyle type alternative to calling sub '4b(ii)b "post processing last column of unified units. ' Probably bad place to put this, other than Speed.. checking / changing units ' If C = ..... ' ' Else ' End If Next C '--- .... 'go to next "Cell" in that table row (next Column we "see" in the table row) Next r '--- 'Go to next row in this table----------------------------| '4b(ii)c Output from Array Let Range("A1").Resize(UBound(Data(), 1) + 1, UBound(Data(), 2) + 1).Value = Data() Columns("A:Z").AutoFit 'Next n 'go back with a new item, n in large collection Object(item) to get next object within and start checking that one out. 'Go to the next table==== Set HTMLdoc = Nothing ' If done then when we no longer need it End Sub '




Reply With Quote
Bookmarks