Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleGet the selected text from a frame within a WebBrowser control by using the control's methods
DescriptionThis example shows how to get the selected text from a frame within a WebBrowser control by using the control's methods in Visual Basic 6.
KeywordsWebBrowser, browser, internet, text, selected text, frame, frameset, frames
CategoriesTips and Tricks, Controls
 
Thanks to Bruce Deam.

Bruce noticed that the example Get all text or the selected text from a WebBrowser control by using the control's methods doesn't work if the selected text is inside a frame. This example searches the Web page's frames to find the selected text.

Function getSelectedDocument searches for a frame that contains selected text. First it checks the selection.Type property of the HTMLDocument that it received as a parameter to see if that object has selected text. If it contains selected text, the function returns that document.

If the document does not contain selected text, the routine loops through the document's frames. For each frame, the function calls itself recursively, passing itself the frame's Document object. If the recursive call returns a document containing selected text, the function passes that document up to its caller.

 
' Find the selected document.
Public Function getSelectedDocument(ByVal DocO As _
    HTMLDocument) As HTMLDocument
Static depth As Long

Dim i As Integer
Dim sub_doc As HTMLDocument
Dim hw2 As HTMLWindow2
Dim doc2 As HTMLDocument

    ' Avoid infinite loops for some deeply nested pages
    If depth < 6 Then depth = depth + 1 Else Exit Function

    If DocO.selection.Type = "Text" Then
        ' Debug.Print "Found selection in " &
        ' DocO.parentWindow.Name
        Set getSelectedDocument = DocO
    ElseIf DocO.frames.length <> 0 Then
        For i = 0 To DocO.frames.length - 1
            Set hw2 = DocO.frames(i)
            ' Debug.Print Space$(depth * 2) & hw2.Name
            Set doc2 = hw2.Document
            Set sub_doc = getSelectedDocument(doc2)
            If Not sub_doc Is Nothing Then
                depth = depth - 1
                Set getSelectedDocument = sub_doc
                Exit Function
            End If
        Next i
    Else
        Set getSelectedDocument = Nothing
    End If
    depth = depth - 1
End Function
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated