Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

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

Status
Not open for further replies.

DeGeneral

IS-IT--Management
Jul 4, 2003
12
GB
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
 
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:
[blue]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 "[URL unfurl="true"]www.microsoft.com"[/URL]
    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[/blue]
 
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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top