INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Any Alternative to "Control F" on a webbrowser component?

Any Alternative to "Control F" on a webbrowser component?

(OP)
My program, written in VB6, needs to find and highlight the location of some text on a webpage which is in a webbrowser component in my program.

I have been using the {ctrl + F} (i.e. ' SendKeys "^f" ') find function. This works in isolation - but only when I tab the focus to the command button and press [Enter] (i.e. the alt + shortcut doesn't work, nor does clicking on the button with mouse). However, when I add in the code for subsequent steps in the program, this find function doesn't work at all. I have tried packaging the part that does work as a separate .exe and .dll but neither work.

I have 2 questions:

1) Does anyone know of any other way to search a webpage (not the HTML source code, the page as it appears in the browser), other than the in-built windows find/replace function?

2) If it is necessary to use the Windows in-built help function, is there any way to access this directly from my VB code, perhaps through an API call, rather than through {ctrl + F} or [Edit]-[Find]? Any sample code which shows this?

Thanks,
DeGeneral

RE: Any Alternative to "Control F" on a webbrowser component?

1) I'd love to be able to tell you to use the following, assuming that 'mywebbrowser' contains a reference to the browser you are interested in:

mywebbrowser.ExecWB OLECMDID_FIND, OLECMDEXECOPT_DONTPROMPTUSER, "wordtofind"

Unfortunately OLECMDID_FIND is one of several ExecWB parameters not defined for the IWebBrowser2::ExecWB() method - so it won't work. You have to go around the houses a little bit. Here's an example of one implementation of a Find/Find Next function. For simplicity it requires you to place a Webbrowser control on a form, and a textbox and two command buttons. You'll also need to add a reference to the Microsoft HTML object library:

CODE

Option Explicit

Private Sub Command1_Click()
    Find WebBrowser1, Text1.Text ' Find
End Sub

Private Sub Command2_Click()
    Find WebBrowser1 ' FindNext
End Sub

Private Sub Form_Load()
    WebBrowser1.Navigate2 "www.microsoft.com"
    Text1.Text = "Microsoft"
    Command1.Caption = "Find"
    Command2.Caption = "Find Next"
End Sub


Private Sub Find(mywebbrowser As WebBrowser, Optional strFind As String = "")
    Static mytextrange As IHTMLTxtRange
    Static TextToFind As String
    
    If strFind <> "" Then ' FindNext
        Set mytextrange = mywebbrowser.Document.body.createTextRange
        TextToFind = strFind
    Else
        mytextrange.moveStart "character"
    End If
    
    With mytextrange
        If .findText(TextToFind) = True Then
            .Select
            .ScrollIntoView True
        Else
            MsgBox "Text not found", vbInformation, App.EXEName
        End If
    End With

End Sub

RE: Any Alternative to "Control F" on a webbrowser component?

(OP)
Thanks strongm.

This works really well for most webpages. However, there is one important webpage I need to access which throws up the message: "object doesn't support this property or method". The offending line of code is: "Set mytextrange = mywebbrowser.Document.body.createTextRange". Any idea why this happens or how I could get around it?

DeGeneral

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close