Consulting

Results 1 to 6 of 6

Thread: Finding text on ie that's next to what you search

  1. #1

    Question Finding text on ie that's next to what you search

    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

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    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
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3

    Thumbs up

    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

  4. #4
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
        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
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  5. #5
    Brilliant, works really well.

    Many thanks for your help on this.

  6. #6
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •