Print Page | Close Window

WebBrowser

Printed From: Codejock Forums
Category: Codejock Products
Forum Name: Controls
Forum Description: Topics Related to Codejock Controls
URL: http://forum.codejock.com/forum_posts.asp?TID=18417
Printed Date: 19 February 2025 at 3:14pm
Software Version: Web Wiz Forums 12.04 - http://www.webwizforums.com


Topic: WebBrowser
Posted By: SteveStraley
Subject: WebBrowser
Date Posted: 20 May 2011 at 8:20pm
Hi,

Does anyone know how, within the AxWebBrowser control, to capture a selected section of text displayed within the control?

Thanks,

Steve



Replies:
Posted By: jpcoffeyXXX
Date Posted: 19 June 2012 at 3:10pm
Steve, the easy answer is,

Private Function RetrieveFrameSelectedTextEasy(wb As WebBrowser) As String
    Dim oDoc As HTMLDocument, tr As IHTMLTxtRange

    Set oDoc = wb.Document
    If LCase$(oDoc.selection.Type) = "text" Then
        Set tr = oDoc.selection.createRange
        RetrieveFrameSelectedText = tr.Text
        Set tr = Nothing
    End If
    Set oDoc = Nothing
End Function


However, in many cases, there are several iFrames on the page and you do not know which iFrame may have selected text.  In this case you would need to use the following,

Private Function RetrieveFrameSelectedText(wb As WebBrowser) As String
    Dim pContainer As olelib.IOleContainer
    Dim pEnumerator As olelib.IEnumUnknown
    Dim pUnk As olelib.IUnknown
    Dim pBrowser As shdocvw.IWebBrowser2
    '======================================
    Dim oDoc As HTMLDocument
    Dim tr As IHTMLTxtRange

    RetrieveFrameSelectedText = vbNullString
    Set pContainer = wb.Document
    ' Get an enumerator for the frames
    If pContainer.EnumObjects(OLECONTF_EMBEDDINGS, pEnumerator) = 0 Then
        Set pContainer = Nothing
        ' Enumerate all the frames
        Do While pEnumerator.Next(1, pUnk) = 0
            On Error Resume Next   'JPC - Edanmo's error handler
            ' Clear errors
            Err.Clear
            ' Get the IWebBrowser2 interface
            Set pBrowser = pUnk
            If Err.Number = 0 Then
                On Error GoTo 0
                'Debug.Print "Private Sub EnumFrames Frame URL: " & pBrowser.LocationURL

                Set oDoc = pBrowser.Document
                If LCase$(oDoc.selection.Type) = "text" Then
                    Set tr = oDoc.selection.createRange
                    RetrieveFrameSelectedText = tr.Text
                    Set tr = Nothing
                    'Don't look for anymore selected text
                    Exit Do
                End If
            End If
        Loop
        Set oDoc = Nothing
        Set pEnumerator = Nothing
    End If
End Function


It requires a reference to Edanmo's type library "OLELIB.TLB - OLE interfaces & functions v1.7" at the top of the page at

http://www.mvps.org/emorcillo/en/code/vb6/index.shtml - http://www.mvps.org/emorcillo/en/code/vb6/index.shtml

Hope this helps.

Sincerely,


John Coffey







Posted By: McKloony
Date Posted: 17 July 2012 at 5:14am
Is it also possible so save the content of a Webbrowser control als MHT or HTML File?

-------------
Product: Xtreme SuitePro (ActiveX) 16.2.5

Platform: XP / Windows 7

Language: Visual Basic 6.0 SP6



Print Page | Close Window

Forum Software by Web Wiz Forums® version 12.04 - http://www.webwizforums.com
Copyright ©2001-2021 Web Wiz Ltd. - https://www.webwiz.net