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
|