(VBA)如何在同一浏览器上导航新的html页面时刷新HTMLDocument对象?

时间:2017-05-31 15:47:25

标签: vba excel-vba excel

我的任务是创建一个登录网站并导航到特定页面的宏,在该页面中可以将html表复制到excel中。我的问题是,当我尝试拉出html表时,我得到了上一个网页的html表。我的想法是我需要更新我的HTMLDocument一些方法。这可能吗?遗憾的是,我无法提供具体的URL,因为它是保密的。

我的代码可以在下面找到。

ActionController::RoutingError (No route matches [GET] "/posts/:id/undefined")...

2 个答案:

答案 0 :(得分:0)

未经测试:

尝试替换

Set eleColtr = HTMLDoc.getElementsByTagName("tr")

用这个:

Set eleColtr = Nothing
For i = 1 To 50
    On Error Resume Next
    Set eleColtr = HTMLDoc.getElementsByTagName("tr")
    If Err.Number = 91 Then
        GoTo Skip
    End If
    Exit For
Skip:
Application.Wait (Now() + TimeValue("00:00:001"))
Next i

我没有测试过这个,所以如果你仍然得到最后一个表的详细信息,那么你需要再次设置HTMLDoc。

答案 1 :(得分:0)

我最终让我的代码工作,感谢大家的帮助。 @Macro Man是正确的,因为我导航到我想要的页面后没有设置DOMdocument。所以我在Browser.navigate之后创建了一个新的DOMdocument实例,但并不是所有这些都需要。出于某种原因,我需要在设置新的DOM文档之前再次导航到我想要的页面。我仍然感到困惑,为什么这有效,但是谁在乎呢。

Browser.navigate ("URL")


Debug.Print "DELAY STARTED"
Application.Wait (Now + TimeValue("0:00:05")) ' delay 5 seconds
Application.Wait (Now + TimeValue("0:00:05")) ' delay 5 seconds
Application.Wait (Now + TimeValue("0:00:05")) ' delay 5 seconds
Application.Wait (Now + TimeValue("0:00:05")) ' delay 5 seconds

Browser.navigate ("URL")'DONT REMOVE OR WILL BREAK
Set WTF = Browser.document ' my new instance of DOMdocument

Debug.Print "Second Delay"
Application.Wait (Now + TimeValue("0:00:05")) ' delay 5 seconds
Application.Wait (Now + TimeValue("0:00:05")) ' delay 5 seconds

'*****************************************************************************************************************************************
'http://www.ozgrid.com/forum/showthread.php?t=184695
Dim eleColtr As MSHTML.IHTMLElementCollection 'Element collection for tr tags
Dim eleColtd As MSHTML.IHTMLElementCollection 'Element collection for td tags
Dim eleRow As MSHTML.IHTMLElement 'Row elements
Dim eleCol As MSHTML.IHTMLElement 'Column elements

Set eleColtr = WTF.getElementsByTagName("tr") 'Find all tr tags
'This section populates Excel
    i = 0 'start with first value in tr collection
    For Each eleRow In eleColtr 'for each element in the tr collection
        Debug.Print "goodnews"
        Set eleColtd = WTF.getElementsByTagName("tr")(i).getElementsByTagName("td") 'get all the td elements in that specific tr
        j = 0 'start with the first value in the td collection
        For Each eleCol In eleColtd 'for each element in the td collection
            Sheets("Sheet1").Range("A1").Offset(i, j).Value = eleCol.innerText 'paste the inner text of the td element, and offset at the same time
            j = j + 1 'move to next element in td collection
        Next eleCol 'rinse and repeat
        i = i + 1 'move to next element in td collection
    Next eleRow 'rinse and repeat