Asesha George
Asesha George

Reputation: 2268

Search specific text from Web page view source and copy full complete line using VBA Macros

I am trying to get a full line of specific text from webpage view source using Excel VBA. I could able to find the text but unable to get in a cell. below is my VBA code

Sub latlong()

Dim IE As Object
Dim html As HTMLDocument

Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.navigate Range("p2")

Do While IE.Busy
    Application.Wait DateAdd("s", 1, Now)
Loop

mes = IE.document.body.innerHTML
If InStr(mes, "var point") = 0 Then
     ActiveSheet.Range("q2").Value = "Not Found"
        Else
            ActiveSheet.Range("q2").Value = "Found"
       End If



 IE.Quit

    Set IE = Nothing

End Sub

I want to copy the full line of specific text below is an example text which I am trying to extract from a web page

var point = new GLatLng(17.71697,083.30275);

I didn't find any suitable answer for my question please help me

Upvotes: 0

Views: 2328

Answers (1)

StoneGiant
StoneGiant

Reputation: 1497

Since you know where the line starts, it remains only to extract the line from start to end. The line ends with a semi-colon. The answer should be this simple:

Sub latlong()

Dim IE As Object
Dim html As HTMLDocument
Dim codeLine as String
Dim startPos as Long

Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.navigate Range("p2")

Do While IE.Busy
    Application.Wait DateAdd("s", 1, Now)
Loop

mes = IE.document.body.innerHTML

startPos = InStr(mes, "var point")
If startPos = 0 Then
    ActiveSheet.Range("q2").Value = "Not Found"
Else
    codeLine = Mid(mes, startPos, InStr(startPos, mes, ";") - startPos)
    ActiveSheet.Range("q2").Value = codeLine
End If

IE.Quit

Set IE = Nothing

End Sub

No RegEx Required. :-)

Upvotes: 1

Related Questions