View Full Version : [vb] How do you pull a specific string from an html doc?

Bry Man
01-31-2005, 05:52 AM

Ive been working on this program for some time now and ive run into a snag, I need to be able to pull a certain string from an html document and set up a conditional statement like.

If string="text" then
Elseif string="bleh" then
End If

Ive done alot of searching on the net and have found one example of how to find text in an html document, the code is below.

Private Sub Command1_Click()
'The command you would use for this
'function should look a bit like
If WebPageContains( _
"Hello how are you today?") = True Then
MsgBox "The webpage contains the text"
End If
End Sub
Private Function WebPageContains(ByVal s As String) As Boolean
Dim i As Long, HTMLElement
'Change the "WebBrowser1" to the
'name of your Web Browser in the
'next two lines of code.
For i = 1 To WebBrowser1.Document.All.length
Set HTMLElement = _
If Not (HTMLElement Is Nothing) Then
If InStr(1, HTMLElement.innerHTML, _
s, vbTextCompare) > 0 Then
WebPageContains = True
Exit Function
End If
End If
Next i
End Function

Only ive changed it to if the text is equal to something then it performs an action, the snag I run into is this, If the string im looking for has a space in it then only the first word before the space is found, not the whole string which is what I need, is there a way that I could perhpas modify the above code to include spaces in the search? Im sorry for the sort of broken up in a rush way I typed this but Ive written out about 7000 lines of code in a day and im a bit fryed and this is like the last thing that I need so Im a bit frustrated.

02-01-2005, 10:46 AM
Here's an alternative: How To Use Regular Expressions in Microsoft Visual Basic 6.0 (http://support.microsoft.com/default.aspx?scid=kb;en-us;818802)