Link to home
Create AccountLog in
Avatar of KentDBerry
KentDBerry

asked on

Using VB 6 or VBA, how to search for text within an HTML Frame for Internet Explorer

Using VB 6 or VBA, how can I search for text within an HTML Frame for Internet Explorer?

For example, what would be the code to search for a string for the following page.

ie.Document.frames("main").navigate "http://www.msn.com"
Avatar of jan_boven_al
jan_boven_al

Hi Again ;-)
I do not really understand why you would want to search within the frame.
Why not open a new explorer (maybe even hidden) and get the content from there?

If you need to know the location of the content of the frame the code below can help you.
However this code is far from bullet proof. If there is an object before the frame you are looking for which does not have a name property then the code will fail.
Any improvements are welcome.
Jan

    Dim ie As InternetExplorer
    Set ie = New InternetExplorer
    Dim X As Integer
    ie.Visible = True
    ie.Navigate "http://lois.co.uk/web/articles/2frames.html"
    'you need to wait for the page to load
    For X = 0 To ie.Document.body.all.Length() - 1
        If ie.Document.body.all(X).Name = "top" Then
            MsgBox ie.Document.body.all(X).src
            Exit For
        End If
    Next

Open in new window

Avatar of KentDBerry

ASKER

thx.  Here is why I need to search within the frame.
I have Excel document with Hyperlinks.  The Hyperlink needs to open a page in IE that has two frames.  I want the text of the Hyperlink to be found in the "Main" frame and the view scrolled in order for user to see the selection.

I can't get the below code to do the FindText part correctly.  Please help to resolve.
    Dim X As Integer
    Dim txt As IHTMLTxtRange
    For X = 0 To ie.Document.body.all.Length() - 1
        If ie.Document.body.all(X).Name = "main" Then
            Set txt = ie.Document.body.all(X).createTextRange  <-Error here
        
            bFound = txt.findText(strCurrentProtocolName)
        
            If bFound = True Then
                txt.MoveStart "character", -1
                txt.findText strCurrentProtocolName
                txt.Select
                txt.ScrollIntoView
                ie.Visible = True
            Else
                MsgBox "Protocol " & strCurrentProtocolName & " was not found on the " & strYear & " Protocol Documents website!", vbInformation + vbOKOnly
            End If
 
            Exit For
        End If
    Next

Open in new window

Hi
I've been doing some research on this one. As far as I could see you will not be able to do this unless you have internet explorer 8.  See http://msdn2.microsoft.com/en-us/library/cc196985(VS.85).aspx
Maybe there you will.
Jan
Thx for your research Jan.  Unfortunately, I need a solution or workaround for Internet Explorer 6 and 7 in our corporate environment.
Kent
I have the feeling that if you could describe your business problem without solution in it; I may be able to help.
But a solution with searching inside a frame (including positioning text) seems unfeasable in interenet explorer 6
Jan
Avatar of Vadim Rapp
Is the problem that you don't know how to get to the innertext (which is ie.Document.frames("main").document.body,innertext) , or you know but get "access denied"?
typo: ie.Document.frames("main").document.body.innertext
I don't know how to get to the innertext, and I need to know the syntax for doing "findtext" on a frame
Here is a sample in VB6 :

'This form needs:
'1 webbrowser control (hit ctrl+t "Microsoft Internet Controls")

Dim myURL As String

Private Sub Form_Activate()
    WebBrowser1.Height = 600
    myURL = "http://home.arcor.de/u101143665/moveend.htm"
    WebBrowser1.Navigate myURL
End Sub


Private Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object, URL As Variant)
DoEvents
If URL = "" Then Exit Sub

Set o = WebBrowser1.Document.All.tags("A")

M = o.length
For r = 0 To M - 1: zz = ""

zz = zz & "Link Index : " & r & " of " & o.length - 1
zz = zz & String(3, vbCrLf)
'
zz = zz & "A . href : " & o.Item(r).href
zz = zz & String(3, vbCrLf)
'
zz = zz & "A . innerhtml : " & o.Item(r).innerhtml
zz = zz & String(3, vbCrLf)
'
zz = zz & "A . outerhtml : " & o.Item(r).outerhtml
zz = zz & String(3, vbCrLf)
MsgBox zz
'
If o.Item(r).innerhtml = "Tool2" Then
    Set w = WebBrowser1.Document.body.createtextrange
       w.movestart "character", 7
       w.moveend "character", -8
    w.Select
    w.ScrollIntoView
End If

Next

End Sub
Thanks.  Please help me to be able get your code to work from within an Excel VBA module.  I am not using a form in this instance.  Rather, the solution needs to be able to work from clicking on a hyperlink from within an Excel worksheet.
I see how the example now works and able to reproduce using a form.  I will try using a browsercontrol on a form to see if I can get my desired functionality this way rather than through IE 6.  May still need help with the Frame issue however.

if instr(ie.Document.frames("main").document.body.innertext, "my text")<>0 then msgbox "found it!"
vb Elmar,

I've almost got your suggestion to work.  I can find the text in a frame called main and scroll into view.  However, I cannot get the text to highlight that it has found.
The below code scrolls the frame with the found text appearing on the first line of the frame.  How do I select the innerHTML text?
    Dim o As Variant
    Dim M As Long
    Dim R As Long
    Set o = frameMain.Document.all.tags("A")
    M = o.Length
    
    For R = 0 To M - 1
        If o.item(R).innerHTML = strCurrentProtocolName Then
            o.item(R).ScrollIntoView
            Exit Function
        End If
    Next R

Open in new window

I modified my sample in VB6 . The sample can get
the text to highlight that it has found  :

'This form needs:
'1 webbrowser control (hit ctrl+t "Microsoft Internet Controls")
Dim myURL As String

Private Sub Form_Activate()
WebBrowser1.Height = 600
myURL = "http://home.arcor.de/u101143665/moveend.htm"
WebBrowser1.Navigate myURL
End Sub

Private Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object, URL As Variant)
DoEvents
If URL = "" Then Exit Sub

Set o = WebBrowser1.Document.All.tags("A")

M = o.length
For r = 0 To M - 1: zz = ""

zz = zz & "Link Index : " & r & " of " & o.length - 1
zz = zz & String(3, vbCrLf)
'
zz = zz & "A . href : " & o.Item(r).href
zz = zz & String(3, vbCrLf)
'
zz = zz & "A . innerhtml : " & o.Item(r).innerhtml
zz = zz & String(3, vbCrLf)
'
zz = zz & "A . outerhtml : " & o.Item(r).outerhtml
zz = zz & String(3, vbCrLf)
MsgBox zz
'
If o.Item(r).innerhtml = "Tool2" Then
Set w = WebBrowser1.Document.body.createtextrange
w.moveToElementText o.Item(r)
w.Select
'w.ScrollIntoView
End If

Next

End Sub
ASKER CERTIFIED SOLUTION
Avatar of vb_elmar
vb_elmar
Flag of Germany image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Perfect.  That is exactly what I needed.  Below is my code and how I implemented it for my needs.  
Public ie As InternetExplorer
Dim strYear As String
Dim strLastYearSearched As String
 
Public Function ieFindAndScroll(strCurrentProtocolName As String)
    On Error GoTo endit
    Dim bFound As Boolean
    Dim HTMLDoc As MSHTML.HTMLDocument
    Dim frameMain As MSHTML.IHTMLWindow2
    
    strYear = Left(Right(strCurrentProtocolName, 6), 4)
    If IsNumeric(strYear) = False Then
        MsgBox "Protocol " & strCurrentProtocolName & " must be renamed using the standard format (i.e. ending in -2008US) in order to search the Protocol Documents website!", vbInformation + vbOKOnly
        Exit Function
    End If
    
beginning:
    If ie Is Nothing Then
        Set ie = New InternetExplorer
        ie.Navigate "http://nafta1.pro.intra/brd/TT/FieldCrops/" & strYear & "/ProtInit/Frame.htm"
        Do Until ie.ReadyState = READYSTATE_COMPLETE
        Loop
        Set HTMLDoc = ie.Document
        Set frameMain = HTMLDoc.Frames.item(0)
        frameMain.Navigate ("http://nafta1.pro.intra/brd/TT/FieldCrops/" & strYear & "/Protocols/SortedByProtocol_Study.html")
        Do Until ie.ReadyState = READYSTATE_COMPLETE
        Loop
        Sleep 500
        ie.Visible = True
        
        strLastYearSearched = strYear
    Else
        If strYear <> strLastYearSearched Then
            ie.Navigate "http://nafta1.pro.intra/brd/TT/FieldCrops/" & strYear & "/ProtInit/Frame.htm"
            Do Until ie.ReadyState = READYSTATE_COMPLETE
            Loop
            
            Set HTMLDoc = ie.Document
            Set frameMain = HTMLDoc.Frames.item(0)
            frameMain.Navigate ("http://nafta1.pro.intra/brd/TT/FieldCrops/" & strYear & "/Protocols/SortedByProtocol_Study.html")
            Do Until ie.ReadyState = READYSTATE_COMPLETE
            Loop
            strLastYearSearched = strYear
            ie.Visible = True
        Else
            Set HTMLDoc = ie.Document
            Set frameMain = HTMLDoc.Frames.item(0)
        End If
    End If
    DoEvents
    
    Dim o As Variant, w As Variant
    Dim M As Long
    Dim R As Long
    Set o = frameMain.Document.all.tags("A")
    M = o.Length
    
    For R = 0 To M - 1
        If o.item(R).innerHTML = strCurrentProtocolName Then
            o.item(R).ScrollIntoView
            Set w = frameMain.Document.body.createTextRange
            w.moveToElementText o.item(R)
            w.Select
 
            frameMain.Focus  'Now force window to scroll back to the left
            Application.SendKeys "{LEFT}{LEFT}{LEFT}{LEFT}{LEFT}", True
            Exit Function
        End If
    Next R
    
    Exit Function
endit:
    Select Case Err.Number
        Case -2147417848 'automation error.  user probably closed ie manually
            Set ie = Nothing
            GoTo beginning
        Case Else
            Debug.Print Err.Number & " " & Err.Description
             MsgBox Err.Number & Err.Description
                 End Select
End Function

Open in new window

Congrats elmar
I'll keep this piece of code for future reference. :-)
Jan