Private Sub Command1_Click()
    Dim objSelection
    Dim objTxtRange
    
    Set objSelection = WebBrowser1.Document.selection
    If Not (objSelection Is Nothing) Then
        Set objTxtRange = objSelection.createRange
        If Not (objTxtRange Is Nothing) Then
            Debug.Print objTxtRange.htmlText
        
            Set objTxtRange = Nothing
        End If
        Set objSelection = Nothing
    End If
End SubPrivate Sub Form_Load()
    WebBrowser1.Navigate "http://www.applevb.com"
End Sub上面是选择后取得html代码,但在实际应用中比如文本框和按钮很难选择,问是否有用焦点方式取html代码的,即焦点在文本框中就取这个控件的html

解决方案 »

  1.   

    可以用document的elementFromPoint(x,y)来取得光标处的对象。
      

  2.   

    下面是一个例子以,点击webbrowser中的页面后显示点击处的内容。供参考。
    Private WithEvents htmDoc As HTMLDocumentPrivate Function htmDoc_onclick() As Boolean
        Dim docName As String
        Dim x As Long, y As Long
        Dim w As Long, h As Long
        Dim offX As Long, offY As Long
        Dim rowID As String
        Dim t As String
        x = htmDoc.parentWindow.event.clientX
        y = htmDoc.parentWindow.event.clientY
        rowID = htmDoc.elementFromPoint(x, y).parentElement.Id
        If rowID = "" Then Exit Function
        t = htmDoc.getElementById(rowID).innerText
        MsgBox t
    End Function
    Private Sub Form_Load()
       WebBrowser1.Navigate2 App.Path & "\click.htm"
       Set htmDoc = WebBrowser1.Document
    End Sub
      

  3.   

    Private WithEvents htmDoc   As HTMLDocument
    提示用户定义类型,要引用什么?
      

  4.   

    引用Microsoft HTML OBject Library程序可运行,但无法取到值不知将htmDoc_onclick() 放到哪里
      

  5.   

    声明Private   WithEvents   htmDoc       As   HTMLDocument 
    后htmDoc中的事件会在编辑环境中的事件框中出现。htmDoc_onclick()会在点击webbrowser中的页面时产生。
      

  6.   

    无证编程:
    htmDoc_onclick 在什么事件中用啊,放到一个but中说 错误htmDoc.parentWindow.event.clientX 对象变量未设置
      

  7.   

    我调试发现需要在WebBrowser1_DocumentComplete而不是load中Set才能正常使用
      

  8.   

    另还有一个问题是如果页中如果没有ID只存在name有什么好的方法取到name值呢,我在其中没有发现可用的方法
    <input type="hidden" name="domains" value="www.applevb.com"></input>
      

  9.   

    document.getElementsByName('domains')[0].val();