当文档完全加载时,我如何使webbrowser只给我一个动作?

时间:2013-03-14 17:06:18

标签: vb.net vba browser

像标题所说:D 当文档完全加载时,我如何使webbrowser只给出一个动作?

我想制作一个标签,显示我做了多少刷新/访问过的网站..如果我放了 webbroser文档中的label1.text = label1.text + 1完成它给了我很多...

(抱歉我的英语不好)

3 个答案:

答案 0 :(得分:0)

WebBrowser甚至为它加载的每个资源(images,css,js,html等)提供了DocumentCompleted。因此,您需要检查事件参数,看看已完成的网址是否是您想要计算的网址。

答案 1 :(得分:0)

在VBA中我会这样做:

Dim ieApp As Object

Dim myURL As String
myURL = "http://www.stackoverflow.com"

Set ieApp = CreateObject("InternetExplorer.Application")

ieApp.Navigate myURL
ieApp.Visible = True

Do
    DoEvents
Loop Until ieApp.ReadyState = 4   'which equals to READYSTATE_COMPLETE

MsgBox "Loading completed"

答案 2 :(得分:0)

您可以创建自己的类,它将包装Internet Explorer对象并捕获它的事件。简短的例子:

标准模块

' add references:
' Microsoft HTML Object Library
' Microsoft Internet Controls

Option Explicit

Private Const URL As String = "http://stackoverflow.com/questions/15415710/how-i-can-make-webbrowser-give-my-only-one-action-when-document-is-completely-lo"

Sub test()
    ' Creates new visible browser instance
    Dim myInternetExplorerObject As MyInternetExplorer
    Set myInternetExplorerObject = New MyInternetExplorer

    myInternetExplorerObject.Visible = True
    myInternetExplorerObject.Navigate URL

    ' Waits until the document is loaded completely
    Do While myInternetExplorerObject.ReadyState <> SHDocVw.tagREADYSTATE.READYSTATE_COMPLETE
        DoEvents
    Loop

    ' Exits browser and closes the open document
    myInternetExplorerObject.Quit
    Set myInternetExplorerObject = Nothing
End Sub

名为MyInternetExplorer的类模块

Option Explicit

Private WithEvents m_internetExplorerObject As InternetExplorer

Private Sub Class_Initialize()
    Set m_internetExplorerObject = New InternetExplorer
End Sub

Private Sub Class_Terminate()
    Set m_internetExplorerObject = Nothing
End Sub

Private Sub m_internetExplorerObject_DocumentComplete(ByVal pDisp As Object, URL As Variant)
'    Event DocumentComplete(pDisp As Object, URL)
'    Member of SHDocVw.InternetExplorer
'    Fired when the document being navigated to reaches ReadyState_Complete.

    Debug.Print "DocumentComplete:"
    Debug.Print VBA.IIf(Not pDisp Is Nothing, pDisp.Name & " : ", "") & URL
End Sub

Private Sub m_internetExplorerObject_NavigateComplete2(ByVal pDisp As Object, URL As Variant)
'    Event NavigateComplete2(pDisp As Object, URL)
'    Member of SHDocVw.InternetExplorer
'    Fired when the document being navigated to becomes visible and enters the navigation stack.

End Sub

Public Sub Navigate(ByRef URL As String, Optional Flags, Optional TargetFrameName, Optional PostData, Optional Headers)
    m_internetExplorerObject.Navigate URL
End Sub

Public Property Get Visible() As Boolean
    Visible = m_internetExplorerObject.Visible
End Property

Public Property Let Visible(ByVal value As Boolean)
    m_internetExplorerObject.Visible = value
End Property

Public Property Get ReadyState() As tagREADYSTATE
    ReadyState = m_internetExplorerObject.ReadyState
End Property

Public Sub Quit()
    m_internetExplorerObject.Quit
End Sub