|
|
|||||||||
|
|||||||||
|
|||||||||
| |
||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Looping through code
I have a need to write some code in VBA for Word.
I already have some code that goes to a website downloads a football table and then pastes/formats it in Word automatically. This works fine for the first table, but there there are 4tables I need in total. Each one has its own web page. How can I loop through each web page and download each table and the sequentially paste/format them into the word document. I have a counter that goes up, and makes this: httppage = "http://www.footyresults.com/league" & League & ".html" So this returns http://www.footyresults.com/league1.html http://www.footyresults.com/league2.html http://www.footyresults.com/league3html http://www.footyresults.com/league4html I want this to be used in the code that determines the webpage to go to. Any suggestions as to how i can loop through the main internet download table code, but each time reassigning the "httppage" to the next html page? I do not want to repeat the full code for each table ! Any suggestions would be really appreciated... Thanks |
|
#2
|
|||
|
|||
|
I'd probably look at using the webbrowser control to pull the page source from each of the web pages.
__________________
====== Doug G ====== I didn't attend the funeral, but I sent a nice letter saying I approved of it. --Mark Twain |
|
#3
|
||||
|
||||
|
Can you post the code that you have so far. If it works for the first one, I'm sure it can be adapted to loop through all four, without replicating the code four times!!
|
|
#4
|
|||
|
|||
|
Hi
Here is the code: Option Explicit Dim ie As InternetExplorer Dim doc As HTMLDocument Dim tr As HTMLTableRow Dim td As HTMLTableCell Dim tbl As HTMLTable Dim blc As HTMLBlockElement Dim doctbl As Table Private Sub CommandButton1_Click() Dim nrow As Integer Set ie = CreateObject("InternetExplorer.Application") ie.Visible = True ie.navigate "http://www.skysports.com/football/league/0,19540,11660,00.html" Do DoEvents Loop While ie.readyState <> READYSTATE_COMPLETE Set doc = ie.Document Set tbl = doc.getElementById("ss-stat-sort") nrow = tbl.Rows.Length - 1 'Exit Sub Set blc = tbl.all.tags("caption").Item(0) ActiveDocument.Range.InsertAfter blc.outerText Dim myrange As Range Set myrange = ActiveDocument.Content myrange.Collapse direction:=wdCollapseEnd Set doctbl = ActiveDocument.Tables.Add(myrange, nrow, 10) Dim i As Integer, x As Integer x = tbl.Rows.Length - 1 Dim col As Integer, j As Integer For i = 2 To x Set tr = tbl.all.tags("tr").Item(i) col = tr.all.tags("td").Length - 2 For j = 2 To col Set td = tr.all.tags("td").Item(j) doctbl.Cell(i, j).Range.Text = td.outerText Next DoEvents Next doctbl.Columns(2).Width = 140 For i = 3 To 10 doctbl.Columns(i).Width = 30 Next End Sub I want to then do the other tables, eg, http://www.skysports.com/football/league/0,19540,11687,00.html then, http://www.skysports.com/football/league/0,19540,11749,00.html etc... I would like to have them sequentially listed one under the other.... if possible. Any help would be very much appreciated. Thanks |
|
#5
|
||||
|
||||
|
In your original post you seem to indicate that the page names will simply increment by one each time, but I can't see any evidence of this in your latest post. Are the suffixes 11687,00 and 11749,00 random or are they fixed?
If the names of the pages are going to include a number that increments, you should be able to put your code in a loop: Code:
Private Sub CommandButton1_Click()
Dim nrow, intCounter As Integer
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
'This assumes that your pages will be named following the convention ..../football/league/[unique number]
For intCounter = 1 To 4
ie.navigate "http://www.skysports.com/football/league/" & intCounter & ".html"
Do
DoEvents
Loop While ie.readyState <> READYSTATE_COMPLETE
'etc.....
Next
End Sub
If however, your page names do not follow the same convention, you could store the names of the pages in an array: Code:
Private Sub CommandButton1_Click()
Dim nrow, intCounter As Integer
Dim pagenames(4) As String
pagenames(0) = "0,19540,11687,00"
pagenames(1) = "0,19540,11749,00"
'etc..
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
For intCounter = 1 To 4
ie.navigate "http://www.skysports.com/football/league/" & pagenames(intCounter) & ".html"
Do
DoEvents
Loop While ie.readyState <> READYSTATE_COMPLETE
'etc.....
Next
End Sub
|
![]() |
| Viewing: ASP Free Forums > Programming > Visual Basic Programming > Looping through code |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|