Find text on loaded html page

teel73

Registered User.
Local time
Today, 15:14
Joined
Jun 26, 2007
Messages
205
Hello,

I hope someone can assist me with this.

I am simply trying to find text on a Web Browser Control htm page.

Using MS Access 2003, I have a form that includes the activeX Web Browser Control. On load of the form I initialize the web like below:

Code:
   Dim strURL As String
    
    strURL = "[URL]http://www.justice.gov/eoir/profcond/chart.htm[/URL]"
    Me.WebBrowser0.Navigate strURL
    Me.WebBrowser0.Silent = True

The page loads fine.

Now, also, on my form is an unbound text box I call: [txtFind] and a command button I call [cmdFind]. I want [cmdFind] to find the first occurrence of the value in [txtFind]. My code below doesn't do anything when I click the [cmdFind] button. Can someone tell me what I'm doing wrong? Thanks in advance.

Code:
    Public oRange As Object
    Public myfindFirst As Boolean
    Public intTextLength As Long

    
    
Private Sub cmdFind_Click()
    Dim sSearch As String
    Dim strText As String
    
    Set oRange = WebBrowser0.Document.body.createTextRange
    intTextLength = Len(oRange.Text)

    If IsNull(Me.txtFind) Then
      MsgBox "Please enter the text.", vbOKOnly + vbExclamation, "Missing Text or Word"
     Exit Sub
    End If
        
        sSearch = txtFind.Text
        Me.WebBrowser0.SetFocus
        
        If oRange.FindText(sSearch, intTextLength) Then
            oRange.SELECT
            oRange.scrollIntoView
            cmdFind.Caption = "Find Next"
            Me.Refresh
            
            
        Else
             MsgBox ("Search string " & txtFind.Text & " not found.")
        End If
        
    Else
        Me.WebBrowser0.SetFocus
        Call oRange.Move("character")
        Me.txtFind.SetFocus
        sSearch = txtFind.Text
        If oRange.FindText(sSearch, intTextLength) Then
            Me.WebBrowser0.SetFocus
            oRange.SELECT
            oRange.scrollIntoView
        Else
            MsgBox ("Finished searching Document for string " & txtFind.Text)
            cmdFind.Caption = "Find"
            Me.Refresh
        End If
    End If
 
Your initial If Else If conditions are messed up dude. Way off.
 
Code:
Public oRange As Object
Public myfindFirst As Boolean
Public intTextLength As Long
 
Private Sub cmdFind_Click()
	Dim sSearch As String
	Dim strText As String
    
	Set oRange = WebBrowser0.Document.body.createTextRange
	intTextLength = Len(oRange.Text)

	If IsNull(Me.txtFind) Then
		MsgBox "Please enter the text.", vbOKOnly + vbExclamation, "Missing Text or Word"
		Exit Sub
	End If
        
        sSearch = txtFind.Text
        Me.WebBrowser0.SetFocus
        
        If oRange.FindText(sSearch, intTextLength) Then
		oRange.SELECT
		oRange.scrollIntoView
		cmdFind.Caption = "Find Next"
		Me.Refresh
        Else
		MsgBox ("Search string " & txtFind.Text & " not found.")
        End If
        
[COLOR="Red"][B]        Else[/B][/COLOR]

        Me.WebBrowser0.SetFocus
        Call oRange.Move("character")
        Me.txtFind.SetFocus
        sSearch = txtFind.Text

        If oRange.FindText(sSearch, intTextLength) Then
		Me.WebBrowser0.SetFocus
		oRange.SELECT
		oRange.scrollIntoView
        Else
		MsgBox ("Finished searching Document for string " & txtFind.Text)
		cmdFind.Caption = "Find"
		Me.Refresh
        End If

[COLOR="red"][B]    	End If[/B][/COLOR]
 
I hope someone can assist me today. I had no luck with the forum yesterday. Maybe today will be better.

I want to create a feature similar to the "find on this page" IE feature.

On my form I have a web browser active x control, a text box called: [text_to_find] and I have a command button called [cmdFindNow]

On load of my form, I navigate to a html page using a web browser control. This function works fine. My code is below:

Code:
Private Sub Form_Load()
    Dim strURL As String
    
    strURL = "[URL]http://www.justice.gov/eoir/profcond/chart.htm[/URL]"
    Me.WebBrowser0.Navigate strURL
    Me.WebBrowser0.Silent = True
End Sub

Now, what I'd like to be able to do is type in a value in [text_to_find], click the command button [cmdFindNow] and then move the focus or cursor to the first occurrence of the text matching the value in [text_to_find].

Below is what I have so far, it returns a run-time error 438 and it errors out on oRange.findText sSearch

Can someone please tell me what I'm doing wrong or what I am missing?

Code:
Private Sub cmdFind_Click()
    Dim sSearch As String
    Dim oRange as Object
    
    If IsNull(Me.txtFind) Then
       MsgBox "Please enter the text.", vbOKOnly + vbExclamation, "Missing Text or Word"
       Me.txtFind.SetFocus
       Exit Sub
    End If
    sSearch = Me.txtFind.Value
        
    If WebPageContains(sSearch) = True Then 
        MsgBox "The webpage contains the text" 'string is in page
        
        Set oRange = WebBrowser0.Document.body.createTextRange
        [B]oRange.findText sSearch
[/B]        oRange.select
        oRange.scrollIntoView
        
    Else
        MsgBox "The webpage doesn't contains the text"
    End If
 
End Sub
 
 
Private Function WebPageContains(ByVal s As String) As Boolean
Dim i As Long, EHTML
For i = 1 To WebBrowser0.Document.All.length
    Set EHTML = WebBrowser0.Document.All.Item(i)
    If Not (EHTML Is Nothing) Then
        If InStr(1, EHTML.innerHTML, s, vbTextCompare) > 0 Then
        WebPageContains = True
        Exit Function
        End If
    End If
Next i
End Function


Thanks in Advance
 
Did you import a custom browser control, or reference a specific dll?

WebBrowser0.Document doesn't exist in the base setup.
 
Code:
Private Sub btnSearch_Click()

    If Len(txtSearch.Value & vbNullString) > 0 Then
    
        WebBrowser0.SetFocus
        
        SendKeys "^f", False
        SendKeys txtSearch.Value, False
        SendKeys "{ENTER}", False
        SendKeys "{ESC}", False
        
    End If
    
End Sub
 
Thank you BlueIshDan! I will try your example.

I am using the activeX control Microsoft Web Browser in ms access 2003.

The class property is Shell.Explorer.2.
 
BlueIshDan .. your example worked perfectly. Thanks so much.
 

Users who are viewing this thread

Back
Top Bottom