getElementsByClassName

secondyankee

New member
Local time
Today, 05:23
Joined
Aug 27, 2014
Messages
2
Hi all,
I'm trying to make a database program that searches for a given address and returns the zip code. I found that the best site to do this is google. The browser opens easily enough, but I'm having trouble with the program reading the <div Class> that the zip is displayed in. Here is my code:
Code:
Private Sub btnTest_Click()
Dim MyHTML_Element As HTMLDocument
Dim MyURL As String
Dim txt As HTMLInputTextElement
txtAddress = "11700 U.S. 380 Cross Roads, TX"

MyURL = "google.com/?gws_rd=ssl#q=" + txtAddress[/FONT][/COLOR]
  [COLOR=black][FONT=Verdana]‘The forum will not let me post links I assure you that the http is in the code[/FONT][/COLOR]
[COLOR=black][FONT=Verdana]
Set mybrowser = New InternetExplorer
mybrowser.Silent = True
mybrowser.Navigate MyURL
mybrowser.Visible = True
Do
Loop Until mybrowser.ReadyState = READYSTATE_COMPLETE
Set HTMLDoc = mybrowser.Document

Dim Doc As HTMLDocument
'variable for document or data which need to be extracted out of webpage

'capture zip code this is the problem area

Dim dd As Variant
dd = HTMLDocument.getElementsByClassName("vk_sh vk_gy")(0).innerText
MsgBox dd

End Sub
[/FONT][/COLOR]

here is the html associated :

<div class="vk_sh vk_gy" style="padding-top: 18px;">11700 U.S. 380 Cross Roads, TX 76227</div>

Any help would be appreciated
 
Last edited:
Try the below code:
Code:
 Dim MyURL As String
  txtAddress = "11700 U.S. 380 Cross Roads, TX"
  
  'MyURL = "google.com/?gws_rd=ssl#q=" + txtAddress
  MyURL = "https://www.google.dk/search?q=" & txtAddress
  
  Set mybrowser = New InternetExplorer
  mybrowser.Silent = True
  mybrowser.Navigate MyURL
  mybrowser.Visible = True
  Do
  Loop Until mybrowser.ReadyState = READYSTATE_COMPLETE
  
  Dim dd As Variant
  dd = mybrowser.Document.getElementsByClassName("vk_sh vk_gy")(0).innerText
  MsgBox dd
 
JHB,
Thank you for the reply.

I did make the change you suggested and it is returning the same runtime error 91 on:

Code:
 dd = HTMLDocument.getElementsByClassName("vk_sh vk_gy")(0).innerText

The error message states:
Object variable or with block variable not set.
 
JHB,
Thank you for the reply.

I did make the change you suggested and it is returning the same runtime error 91 on:

Code:
 dd = HTMLDocument.getElementsByClassName("vk_sh vk_gy")(0).innerText
The error message states:
Object variable or with block variable not set.
It is not the code I provide!
Mine is for that line:
Code:
 dd = [B][COLOR=Red]mybrowser.Document.[/COLOR][/B]getElementsByClassName("vk_sh vk_gy")(0).innerText
Replace all of your code with the whole block of code I gave you.
 

Users who are viewing this thread

Back
Top Bottom