VBA to select list in IE - excel

I've been trying to use VBA to open an IE window and select from one of the dropdown list showing bwin but failed. Any advice?
Private Sub OKButton_Click()
Dim ie As Object
Set ie = CreateObject("InternetExplorer.Application")
With ie
.Silent = True
.Visible = True
.Navigate "http://www.yahoo.com/"
End With
While ie.ReadyState <> 4 Or ie.Busy: DoEvents: Wend
Set AvailableLinks = oIE.document.getelementbyid("list-listing").getelementsbytagname("a")
For Each cLink In AvailableLinks
If cLink.innerhtml = "????" Then
cLink.Click
End If
Next cLink
End Sub

Related

Retrieve the URL from the Internet Explorer

i am struggling on the following: I try to open an URL(Link), become redirected to a new URL and retrieve this new URL into a Cell in my excel worksheet.
I have written the following code but it is not retrieving the new URL and is not quitting the Internet Explorer at the end:
Sub Get_URL()
Dim ISIN As String
Dim Link As String
Dim IE As Object
ISIN = Range("A1").Value
Link = "https://www.finanzen.net/suchergebnis.asp?_search=" & ISIN
Set IE = CreateObject("InternetExplorer.Application")
With IE
.Visible = True
.Navigate Link
End With
Range("A2") = IE.LocationURL
Set IE = Nothing
IE.Quit
End Sub
The ISIN in A1 is KYG875721634.
I would be very glad if some of you guys could find the problem. Thank you very much!
Greetings, Robin
This works. Read the comments please:
Sub Get_URL()
Dim ISIN As String
Dim Link As String
Dim IE As Object
'ISIN = Range("A1").Value
ISIN = "KYG875721634"
Link = "https://www.finanzen.net/suchergebnis.asp?_search=" & ISIN
Set IE = CreateObject("InternetExplorer.Application")
With IE
.Visible = True
.Navigate Link
End With
'You must wait to load the page
Do While IE.readystate <> 4: DoEvents: Loop
'Range("A2") = IE.LocationURL
MsgBox IE.LocationURL
'First quit the IE
'because this step needs the reference ;-)
IE.Quit
Set IE = Nothing
End Sub
There're two issues in your code:
You need to add a wait to load page.
Do Until IE.readyState = 4
DoEvents
Loop
First quit IE. If you first set IE to Nothing then there will be no reference to IE when quit.
IE.Quit
Set IE = Nothing
The final code is like this which can work well:
Sub Get_URL()
Dim ISIN As String
Dim Link As String
Dim IE As Object
ISIN = Range("A1").Value
Link = "https://www.finanzen.net/suchergebnis.asp?_search=" & ISIN
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.Navigate Link
Do Until IE.readyState = 4
DoEvents
Loop
Range("A2") = IE.LocationURL
IE.Quit
Set IE = Nothing
End Sub
Result:

Perform operations in different IE tabs

I would like to search for city in one IE tab then open my generated link in another tab. If I run these operations separately everything work however I am not able to make it work all together. What is possibly wrong?
Debugger points to .Document.getElementsByName("q")(0).Value = "Dublin" saying
Run-time error '-2147467259 (80004005)': Method 'Document' of object
'IWebBrowser 2' failed
Here is my code:
Option Explicit
Sub GetFlightRates()
Dim appIE As Object
Set appIE = CreateObject("internetexplorer.application")
With appIE
.Navigate "https://maps.google.com"
.Document.getElementsByName("q")(0).Value = "Dublin"
.Document.getElementsByClassName("searchbox-searchbutton")(0).Click
.Navigate "https://www.google.co.in/flights/", CLng(2048) '2nd
.Visible = True
End With
Do While appIE.Busy
DoEvents
Loop
'appIE.Quit
Set appIE = Nothing
End Sub
You need a page load wait before attempting to access .document
Option Explicit
Sub OpenTabs()
Dim appIE As Object
Set appIE = CreateObject("internetexplorer.application")
With appIE
.Visible = True
.navigate "https://maps.google.com"
While .Busy Or .readyState < 4: DoEvents: Wend
.document.getElementsByName("q")(0).Value = "Dublin"
.document.getElementsByClassName("searchbox-searchbutton")(0).Click
.navigate "https://www.google.co.in/flights/", CLng(2048) '2nd
While .Busy Or .readyState < 4: DoEvents: Wend
End With
Stop
'appIE.Quit
Set appIE = Nothing
End Sub

how to select a specific dropdown element in webpage by excel vba

Currently, I am working on a webpage to extract some data related to my work. Here I am facing a problem while selecting the drop-down menu in the production list. I want to select the "All" option in the drop-down list. By default, it takes only 25 values in the production table.
Here is my code
Sub Production_table()
Dim objIE As InternetExplorer
Dim ele As Object
Dim ele2 As Object
Dim ele3 As Object
Dim s As String
Dim i As Integer
i = 2
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Do While Range("A" & i).Value <> ""
Set objIE = New InternetExplorer
objIE.Visible = True
source = "https://secure.conservation.ca.gov/WellSearch/Details?api=01120716&District=&County=&Field=&Operator=&Lease=&APINum=&address=&ActiveWell=false&ActiveOp=false&Location=&sec=&twn=&rge=&bm=&PgStart=0&PgLength=10&SortCol=6&SortDir=asc&Command=Search
objIE.navigate source"
Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
Sheets.Add
ActiveSheet.Name = "Data"
On Error Resume Next
Set ele2 = objIE.document.getElementById("productionTable_length"). _
getElementsByTagName("label")
For Each ele3 In ele2
If ele3.Value = -1 Then
ele3.Focus
ele3.Selected = True
ele3.Click
Exit For
End If
Next
webpage link-- https://secure.conservation.ca.gov/WellSearch/Details?api=01120716&District=&County=&Field=&Operator=&Lease=&APINum=&address=&ActiveWell=false&ActiveOp=false&Location=&sec=&twn=&rge=&bm=&PgStart=0&PgLength=10&SortCol=6&SortDir=asc&Command=Search
Please help me on the same.
I always recommend using Chrome Browser for such cases as it let's you inspect the element in a userfriendly way.
Try this
Sub Sample()
Dim objIE As Object, ele As Object, opt As Object
Set objIE = CreateObject("InternetExplorer.Application")
objIE.Visible = True
Source = "https://secure.conservation.ca.gov/WellSearch/Details?api=01120716&District=&County=&Field=&Operator=&Lease=&APINum=&address=&ActiveWell=false&ActiveOp=false&Location=&sec=&twn=&rge=&bm=&PgStart=0&PgLength=10&SortCol=6&SortDir=asc&Command=Search"
objIE.navigate Source
Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
Set ele = objIE.document.getElementsByTagName("select")
For Each opt In ele
If opt.getAttribute("name") = "tblWellRecords_length" Then
opt.Focus
opt.Value = -1
Exit For
End If
Next opt
End Sub
I was looking for the "All" element to click on the production data. it's changing the value of the drop-down if I fix the attribute name as "productionTable_length" but it's not clicking the element. If it is clicked then the total number of shows will be changed from 1-25 to 1-166. Please help me on that.... – Partha 2 hours ago
You did not say about that :) Your question was how to select a specific dropdown element in webpage by excel vba For which I gave you the answer. For what you want, there is a different approach.
Try this
Sub Sample()
Dim objIE As Object, source As String, script As String
Set objIE = CreateObject("InternetExplorer.Application")
objIE.Visible = True
source = "https://secure.conservation.ca.gov/WellSearch/Details?api=01120716&District=&County=&Field=&Operator=&Lease=&APINum=&address=&ActiveWell=false&ActiveOp=false&Location=&sec=&twn=&rge=&bm=&PgStart=0&PgLength=10&SortCol=6&SortDir=asc&Command=Search"
objIE.navigate source
Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
script = "$('select[name=productionTable_length]').val('-1'); $('select[name=productionTable_length]').trigger('change');"
objIE.document.parentWindow.execScript script, "jscript"
End Sub

Navigating through websites

My code is able to interact with the first webpage it brings up, but then as soon as i navigate away from it, my code gives me a '424' object required error. Here is an example.
EX: go to yahoo.com - search for 'Earth' - click on the Yahoo logo to return to the yahoo homepage
Sub InternetPractice()
Dim ie As InternetExplorer
Set ie = New InternetExplorer
ie.Visible = True
ie.navigate "https://www.yahoo.com"
Do While ie.Busy = True Or ie.readyState <> 4: DoEvents: Loop
ie.document.getElementById("uh-search-box").Value = "Earth"
ie.document.getElementById("uh-search-button").Click
Do While ie.Busy = True Or ie.readyState <> 4: DoEvents: Loop
ie.document.getElementById("logo").Click
End Sub
Using VBA with the internet is new to me and any help is appreciated!
EDIT:
I think you are not giving enough time for ie to load... It is working for me.
So you can try this code that gives more time and try to load 3 times before aborting.
Option Explicit
Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Sub InternetPractice2()
Dim IE As InternetExplorer
Dim HTMLDoc As HTMLDocument
Dim logo As Object
Dim n As Long
Set IE = New InternetExplorer 'Set IE = CreateObject("InternetExplorer.Application")
With IE
.Silent = True
.Visible = True
.Navigate "https://www.yahoo.com"
End With
WaitIE IE, 5000
Set HTMLDoc = IE.Document
HTMLDoc.getElementById("uh-search-box").Value = "Earth"
HTMLDoc.getElementById("uh-search-button").Click
load:
WaitIE IE, 10000
Set logo = HTMLDoc.getElementById("logo")
If Not logo Is Nothing Then
logo.Click
ElseIf logo Is Nothing And n < 4 Then
n = n + 1
GoTo load
End If
WaitIE IE, 5000
'Set ie to Nothing
IE.Quit
Set IE = Nothing
End Sub
Sub WaitIE(IE As Object, Optional time As Long = 250)
'Code from: https://stackoverflow.com/questions/33808000/run-time-error-91-object-variable-or-with-block-variable-not-set
Dim i As Long
Do
Sleep time
Debug.Print CStr(i) & vbTab & "Ready: " & CStr(IE.READYSTATE = 4) & _
vbCrLf & vbTab & "Busy: " & CStr(IE.Busy)
i = i + 1
Loop Until IE.READYSTATE = 4 Or Not IE.Busy
End Sub
Original Answer
Try closing ie in the end of the code and add some delay, because if you step the code with F8, you will see that it works:
Sub InternetPractice2()
Dim ie As InternetExplorer
Set ie = New InternetExplorer
ie.Visible = True
ie.navigate "https://www.yahoo.com"
Do While ie.Busy = True Or ie.READYSTATE <> 4: DoEvents: Loop
ie.Document.getElementById("uh-search-box").Value = "Earth"
ie.Document.getElementById("uh-search-button").Click
'Add delay After .Click
Do While ie.Busy = True Or ie.READYSTATE <> 4: DoEvents: Loop
Application.wait Now+Timevalue("00:00:05")
ie.Document.getElementById("logo").Click
'Set ie to Nothing
Set ie = Nothing
End Sub
Also close all iexplorer.exe on Windows Task Manager.

IE.Document.getElementsByName("name").Value = "Value" not working

When I try and do this, it throws a number of errors at me depending on other bits of my code, such as the method is not associated with that object or an unspecified error.
I am trying to go down a list of Excel cells with strings and use a web form to search for those strings.
My code currently looks like this:
Sub YSF_automation()
Application.ScreenUpdating = False
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.Navigate "URL"
While IE.Busy
DoEvents
Wend
IE.Document.getElementsByName("ElementName")(0).Value = "test"
End Sub
Try the below
Option Explicit
Dim IE as Object
Sub YSF_automation()
Application.ScreenUpdating = False
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.Navigate "URL"
While IE.Busy
DoEvents
Wend
set document = IE.document
with document
.getElementsByName("ElementName")(0).value="test"
end with
End Sub

Resources