GetElementById in VBA - vba

I have a problem with entering data from Excel into a web browser using the code I found on the net and customized it to my needs.
He reports error number 424 to me and I don't know how to solve it.
Is it possible that there is a blockage on this site during automatic data entry?
This is the code (inspect) from the site:
input id="input_form1:j_idt26" name="input_form1:j_idt26" type="text" class="form-control searchbox"
Sub SearchBot()
Dim objIE As InternetExplorer 'special object variable representing the IE browser
Dim aEle As HTMLLinkElement 'special object variable for an <a> (link) element
Dim y As Integer 'integer variable we'll use as a counter
Dim result As String 'string variable that will hold our result link
'initiating a new instance of Internet Explorer and asigning it to objIE
Set objIE = New InternetExplorer
'make IE browser visible (False would allow IE to run in the background)
objIE.Visible = True
'navigate IE to this web page (a pretty neat search engine really)
objIE.navigate "https://nbs.rs/sr_RS/drugi-nivo-navigacije/servisi/registar-menica/"
'wait here a few seconds while the browser is busy
Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
'in the search box put cell "A2" value, the word "in" and cell "C1" value
objIE.document.getElementById("input_form1:j_idt26").Value = _
Sheets("Sheet1").Range("A2").Value
objIE.document.getElementById("input_form1:j_idt21").Value = _
Sheets("Sheet1").Range("A3").Value

The elements you are looking for is hiding in an iframe so it's abit tricky but this should work:
Sub SearchBot()
Dim objIE As InternetExplorer 'special object variable representing the IE browser
Dim aEle As HTMLLinkElement 'special object variable for an <a> (link) element
Dim y As Integer 'integer variable we'll use as a counter
Dim result As String 'string variable that will hold our result link
'initiating a new instance of Internet Explorer and asigning it to objIE
Set objIE = New InternetExplorer
'make IE browser visible (False would allow IE to run in the background)
objIE.Visible = True
'navigate IE to this web page (a pretty neat search engine really)
objIE.navigate "https://nbs.rs/sr_RS/drugi-nivo-navigacije/servisi/registar-menica"
'wait here a few seconds while the browser is busy
Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
Dim ieDoc As MSHTML.HTMLDocument
Set ieDoc = objIE.Document
Dim iframeDoc As MSHTML.HTMLDocument
Set iframeDoc = ieDoc.frames(0).Document
'in the search box put cell "A2" value, the word "in" and cell "C1" value
iframeDoc.getElementById("input_form1:j_idt26").Value = _
Sheets("Sheet1").Range("A2").Value
iframeDoc.getElementById("input_form1:j_idt21").Value = _
Sheets("Sheet1").Range("A3").Value
End Sub

Related

VBA can't use getattributebyclass for a specific class

My idea is to get the href/link from this website (https://www.fundinfo.com/en/LandingPage?apiKey=9247bf95-6dfd-3915-3c82-1c9f2ae6fe20&query=LU0329593007) and download the pdf that i need.
html
So based on the screenshot above, i guess i can get the link by using getattributebyclass and class name = "fancybox". But finally nothing can be extracted.
Attach the code here. Not sure if popover hides the code/content? Can anyone help? Many thanks!
Sub GrabLastNames2()
'dimension (set aside memory for) our variables
Dim objIE As InternetExplorer
Dim ele As Object
Dim y As Integer
Dim result As String
'start a new browser instance
Set objIE = New InternetExplorer
'make browser visible
objIE.Visible = True
'navigate to page with needed data
objIE.navigate "https://www.fundinfo.com/en/LandingPage?apiKey=9247bf95-6dfd-3915-3c82-1c9f2ae6fe20&query=LU0329593007"
'wait for page to load
Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
y = 1
'look at all the 'tr' elements in the 'table' with id 'myTable',
'and evaluate each, one at a time, using 'ele' variable
For Each l In objIE.document.getElementsByClassName("fancy")
result = l.outerHTML
Sheets("Sheet1").Range("A" & y) = result
y = y + 1
Next
MsgBox "Done"
End Sub

The remote server machine does not exist or is unavailable for specific website

I tried to open website using below VBA code:
Dim IE As Object
Dim doc As Object
Dim strURL As String
Set IE = CreateObject("InternetExplorer.Application")
With IE '
.Visible = True
.navigate "https://Google.com"
Do Until .readyState = 4
DoEvents
Loop
It's working for website like "google" etc.
But when I tried to open specific site like my company PLM " Agile (https://agileplm.XXXX.com/Agile/default/login-cms.jsp)" throwing error
"The remote server machine does not exist or is unavailable"
I could open the web page on explorer but throwing error while executing from below line
Do Until .readyState = 4
DoEvents
Loop
Is this due to any protection over site or not?
I used early binding on this and created the object InternetExplorerMedium rather than InternetExplorer and it seemed to work.
The issue is that Internet Explorer disconnects from VBA (In my case some internal security settings). The solution is to reconnect Internet Explorer to VBA:
Include this code after the line IE is not responsive anymore and the existing Internet Explorer window will be assigned to = IEObject1
Dim shellWins As SHDocVw.ShellWindows
Dim explorer As SHDocVw.InternetExplorer
Set shellWins = New SHDocVw.ShellWindows
For Each explorer In shellWins
If explorer.Name = "Internet Explorer" Then
Set IEObject1 = explorer
Debug.Print explorer.LocationURL
Debug.Print explorer.LocationName
End If
Next
Set shellWins = Nothing
Set explorer = Nothing
If you have several IE windows open then this code picks the last one. You can choose between the windows by using the URL or LocationName if you need several open windows.
Try to make a test with example code below may help you to solve your issue.
Sub Automate_IE_Load_Page()
'This will load a webpage in IE
Dim i As Long
Dim URL As String
Dim IE As Object
Dim objElement As Object
Dim objCollection As Object
'Create InternetExplorer Object
Set IE = CreateObject("InternetExplorer.Application")
'Set IE.Visible = True to make IE visible, or False for IE to run in the background
IE.Visible = True
'Define URL
URL = "https://agileplm.xxxx.com/Agile/default/login-cms.jsp"
'Navigate to URL
IE.Navigate URL
' Statusbar let's user know website is loading
Application.StatusBar = URL & " is loading. Please wait..."
' Wait while IE loading...
'IE ReadyState = 4 signifies the webpage has loaded (the first loop is set to avoid inadvertently skipping over the second loop)
Do While IE.ReadyState = 4: DoEvents: Loop 'Do While
Do Until IE.ReadyState = 4: DoEvents: Loop 'Do Until
'Webpage Loaded
Application.StatusBar = URL & " Loaded"
'Unload IE
Set IE = Nothing
Set objElement = Nothing
Set objCollection = Nothing
End Sub
Reference:
Automate Internet Explorer (IE) Using VBA
If your issue persist than try to provide a detailed information, Whether you are having issue with opening the page or you got an error while checking the ready state of IE. We will try to provide further suggestions.
Dim IE As Object
Dim doc As Object
Dim strURL As String
Set IE = CreateObject("InternetExplorer.Application")
With IE
.Visible = True
.navigate "https://Google.com"
End With
For Each win In CreateObject("Shell.Application").Windows
If win.Name Like "*Internet Explorer" Then
Set IE = win: Exit For
End If
Next
With IE
Do Until .readyState = 4
DoEvents
Loop

How to enter a website address using VBA and search

I know this may seem easy. I have already entered a code to try and get this to work, but ran into one problem. The format on the link below is the same for all city and states. As long as you can type the name of the city ("City_Search") and the State ("State_Search") you should be able to access the website with the information as seen below.
I have attached the formula I am using below. If anyone can assist me with the search I would appreciate it.
Sub SearchBot1()
'dimension (declare or set aside memory for) our variables
Dim objIE As InternetExplorer 'special object variable representing the IE browser
Dim aEle As HTMLLinkElement 'special object variable for an <a> (link) element
Dim HTMLinputs As MSHTML.IHTMLElementCollection
'initiating a new instance of Internet Explorer and asigning it to objIE
Set objIE = New InternetExplorer
'make IE browser visible (False would allow IE to run in the background)
objIE.Visible = True
'navigate IE to this web page (a pretty neat search engine really)
objIE.navigate "https://datausa.io/profile/geo/" & Range("City_Search").Value & "-" & Range("State_Search").Value
'wait here a few seconds while the browser is busy
Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
End Sub
The Idea would be for me to type any city into excel and once I hit run on a macro it will go to the site and search for the towns data. I have added a link below as an example of the page I am looking to get when I search.
https://datausa.io/profile/geo/hoboken-nj/
You need to hyphenate cities that have spaces in their title. Counties need to be the correct abbreviation and both are case sensitive i.e. need to be all lower case. So you need to add these hyphens, if missing, using a function like Replace in vba, to swop Chr$(32) with "-" or Chr$(45), and potentially LCase$ to convert to lowercase.
You should also fully qualify the range with the worksheet you intend to use.
With data already in correct format in cell:
E.g. with los-angeles-ca or los-angeles-county-ca in a cell.
Option Explicit
Public Sub SearchBot1()
Dim objIE As InternetExplorer, aEle As HTMLLinkElement
Dim HTMLinputs As MSHTML.IHTMLElementCollection
Set objIE = New InternetExplorer
'e.g. https://datausa.io/profile/geo/los-angeles-ca/
With objIE
.Visible = True
.navigate "https://datausa.io/profile/geo/" & Range("City_Search").Value & "-" & Range("State_Search").Value
Do While .Busy = True Or .readyState <> 4: DoEvents: Loop
Stop
' .Quit '<== Uncomment me to close browser at end
End With
End Sub
Adding hyphens:
If you had los angeles, not los-angeles, in a cell:
Replace$(Range("City_Search").Value, Chr$(32), Chr$(45))
Lowercase and hyphen:
To be really safe you could convert to lowercase aswell to handle any upper case letters in the cell you are referencing e.g.
For Los Angeles use: Replace$(LCase$(Range("City_Search").Value)
Option Explicit
Public Sub SearchBot1()
Dim objIE As InternetExplorer, aEle As HTMLLinkElement
Dim HTMLinputs As MSHTML.IHTMLElementCollection, ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Sheet1")
Set objIE = New InternetExplorer
'e.g. https://datausa.io/profile/geo/los-angeles-ca/
With objIE
.Visible = True
.navigate "https://datausa.io/profile/geo/" & ws.Range("City_Search").Value & "-" & ws.Range("State_Search").Value
Do While .Busy = True Or .readyState <> 4: DoEvents: Loop
Stop
' .Quit '<== Uncomment me to close browser at end
End With
End Sub
That gets you to the pages. What you do then......
DID you know that this website has its own data-search API?
And you can also extract data using a background object instead of creating an Internet Explorer?
For instance:
Sub getCityData()
''' Create a background server connection
Dim myCon As Object: Set myCon = CreateObject("MSXML2.ServerXMLHTTP.6.0")
''' Open a connection string with the DataUSA API and basic request for (geo, place, population)
myCon.Open "GET", "http://api.datausa.io/api/?show=geo&sumlevel=place&required=pop"
myCon.send ''' Send the request
''' Dataset in the ResponseText is HUGE so for demo show first 5000 characters
Sheet1.Range("A1").Value2 = Left(myCon.responseText, 5000)
End Sub
That will pull the ENTIRE DATA SET for every "place" in America with its population for every year from 2013 onwards in about a second. It will place the first 5000 characters of the dataset in to cell A1 on Sheet1 (I recommend putting this in a new Excel file).
I don't have time to learn the site's API but it seems to have good documentation On github and the responses come back in JSON format - if you really want to make a powerful excel interface use their API with background connections - they have so much data for the USA at your fingertips

Error 91 Search and scrape web

I am new to VBA, and trying to put together a code that will allow me to search a website for bond information, which is listed in my excel file, and pull back the bond's issue date. I have been able to get to the website, and using F8 to execute the code manually, the code appears to work fine. However, when I run the macro, I get
Error 91: Object variable or With block variable not set.
I am unsure how to fix this issue, and I have tried to find earlier answers, as there is a lot of info on error 91. But none seem to be able to help with my specific problem.
Please assist, thanks.
Dim objIE As InternetExplorer 'special object variable representing the IE browser
Dim datelabel As String
Dim x As String 'for the CUSIP number
Dim y As Integer 'integer variable we'll use as a counter
Dim result As String 'string variable that will hold our result link
'initiating a new instance of Internet Explorer and asigning it to objIE
Set objIE = New InternetExplorer
'make IE browser visible (False would allow IE to run in the background)
objIE.Visible = False
'navigate IE to this web page
objIE.navigate "https://emma.msrb.org/Search/Search.aspx"
'wait here a few seconds while the browser is busy
Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
'DATA SCRAPING Portion
Dim Doc As HTMLDocument
Set Doc = objIE.document
'get issue date
datelabel = Doc.getElementsByClassName("value")(0).innerText 'HERE IS WHERE I HAVE MY PROBLEM
'MsgBox datelabel
End If
Loop
'close the browser
objIE.Quit
This is the html that I am trying to pull:
span class="value"
from:
https://emma.msrb.org/SecurityView/SecurityDetails.aspx?cusip=ACDB05F7DCC14B929AC9D2D082A3D9AE0

vba internet explorer delete object

I'm making simple vba script witch is gonna open IE, get some informations and exit. Script open IE 35 times without problem, but after that it, works, but doesn't open IE anymore.
Sub Test()
for x=1 to 50
Dim objIE As Object
Set objIE = New InternetExplorer
Set objIE = New InternetExplorerMedium
objIE.Visible = True
objIE.Navigate2 "http://www.google.com"
objIE.Visible = False
Set objIE = Nothing
next x
End Sub
I don't really get what you want do with your code, but here some tips that might help to fix the issue:
You can take both the declaration and the set of your browser out of the loop, you don't need to declare it and set it each time:
Dim objIE As Object
Set objIE = New InternetExplorerMedium
Why are you setting objIE as New InternetExplorer, if you're right after setting it again as New InternetExplorerMedium? That action is useless.
Between the objIE.Navigate2 "http://www.google.com" and the Set objIE = Nothing you should probably wait some time, at least with a Do While objIE.busy Loop, because you don't even give the browser the time to load the document and you already destroy it.
The Set objIE = Nothing can also be put out of the loop, you can re-use the same browser to navigate as many links as you want. Also, don't forget to quit it first before destroying it from memory with objIE.quit
If it works 35 times and keep on "working but doing nothing" starting from the 36th, wonder about the links are corrupted when you get there.
You can check that with a Debug.Print on the link you're going to navigate each time.
This is the basic structure to automate IE with VBA.
Option Explicit
Sub PAO()
Dim IE As InternetExplorer
Dim url As String
Const url$ = "http://www.google.com"
Set IE = CreateObject("internetexplorer.application")
IE.Visible = True
IE.navigate url
Set IE = Nothing
End Sub
Also, here is a Wait function that can help you to wait for the webpage to load.
Function IEWait(t As Long)
Do While IE.Busy
Application.Wait DateAdd("s", t, Now)
Loop
End Function
This is how you use the function:
IEWait (1)
Do Until IE.readyState = READYSTATE_COMPLETE: DoEvents: Loop
Hope this will help you but let me know if you have any question.