PDA

View Full Version : [SOLVED] Finding text on ie that's next to what you search



chacanger
02-15-2011, 01:10 PM
Hi

The other day I asked if there was a way to search for text on an ie page without exporting it to Excel, to which I was told InStr would work, I know have another question which relates to the same thing but needs to do something else too, Is it possible to search for the text that is next to what you search for...

by this I mean if you had a web page that said... "My name is chacanger" and you wanted to search for the My name is part (as the name would change), then get it to tell you what was the text to the right of it, so it would produce an answer of "Chacanger"


Sub TextToRight()
Dim ie As InternetExplorer
Set ie = New InternetExplorer
'####Search for Web Page####
ie.Navigate "http://www.google.co.uk/search?client=opera&rls=en&q=google&sourceid=opera&ie=utf-8&oe=utf-8&channel=suggest"
'####Loop until fully loaded####
Do Until ie.ReadyState = READYSTATE_COMPLETE
Loop
FindText = InStr(1, ie.Document.body.innerhtml, "The local version")
MsgBox FindText
If FindText > 0 Then
'####Needs to find the text after the word version and place in MsgBox####
MsgBox "The text exists"
Else
MsgBox "The text dosen't exist"
End If
ie.Quit
Set ie = Nothing
End Sub

The text next to it can vary in size on my project but it stays within the same line.

Any suggestions would be greatly appreciated

mdmackillop
02-15-2011, 01:25 PM
Something like


Sub TextToRight()
Dim ie As InternetExplorer
Dim txt
Dim ToFind As String
Set ie = New InternetExplorer
'####Search for Web Page####
ie.Navigate "http://www.vbaexpress.com/forum/showthread.php?t=36154"
'####Loop until fully loaded####
Do Until ie.ReadyState = READYSTATE_COMPLETE
Loop
ToFind = "My name is "
tof = Len(ToFind)
findtext = InStr(1, ie.Document.body.innerhtml, ToFind)
txt = Mid(ie.Document.body.innerhtml, findtext + tof, 100)
If findtext > 0 Then
'####Needs to find the text after the word version and place in MsgBox####
MsgBox Split(txt)(0)
Else
MsgBox "The text dosen't exist"
End If
ie.Quit
Set ie = Nothing
End Sub

chacanger
02-15-2011, 02:34 PM
Thanks for the help on this mdmackillop, this is great, just the solution I need, if the value next to it I need to capture has spaces and multiple words in it for example chacanger" and you wanted to search for the how would I be able to do it?

Thanks

mdmackillop
02-15-2011, 03:15 PM
If findtext > 0 Then
'####Needs to find the text after the word version and place in MsgBox####
Dim x As Long, i as Long, msg As String
x = InputBox("No. of words to return")
For i = 0 To x - 1
msg = msg & Split(txt)(i) & " "
Next
MsgBox msg
Else
MsgBox "The text dosen't exist"
End If

chacanger
02-15-2011, 03:39 PM
Brilliant, works really well.

Many thanks for your help on this.:thumb

rugbykorn
03-28-2013, 06:57 AM
Hi Chacanger,

I am a novice at VBA and brand new to using it with IE. I was hoping you might be able to help me. Can you explain how to properly determine the area to search when using the inStr function? You used InStr(1, ie.Document.body.innerhtml, ToFind) but don't know that the area I want to search is "ie.Document.body.innerhtml". Are there standard document sections that I can try or is it possible to open a "properties" window or something to determine which section of a IE page I am viewing?

For some background, I am going to be doing the same thing you were: I need to find the text "Employee Name:" and then pull the text that is next to it. I will be pulling this from XPS documents opened in IE. I find that I can open the XPS but the "ie.Document.body.innerhtml" in your inStr left me scratching my head. I will do some research to try and figure out when to use what string segment but I was hoping you may have a quick answer.

Thanks in advance!

RugbyKorn