Select all options in HTML using VBA - html

So I am trying to select all the options on a website using VBA.
the options sometimes changes but i want to select all of them.
the below is the html code for the selection menu.
<select class="locationSelect" id="ReceiptLocations" multiple="multiple" name="ReceiptLocations" size="10"><option value="7">Bayhurst 1</option>
<option value="157">Union Parkway Belt</option>
<option value="260">Union SSMDA</option>
<option value="261">Union WDA</option>
<option value="244">Welwyn</option>
<option value="83343">Whitewood Receipt</option>
</select>
My current code in excel is as follow:
Sub extract()
Dim oIE As Object
Dim ohtml As HTMLDocument
Dim objOption As Object
Set oIE = CreateObject("InternetExplorer.Application")
oIE.Navigate ("http://itbidfloors.transcanada.com/")
oIE.Visible = True
' Wait while IE loading
Do
DoEvents
Loop Until oIE.ReadyState = 4
For Each objOption In oIE.Document.getElementsByClassName("locationSelect").getElementsByID("ReceiptLocations").Options
objOption.Selected = True
Next
End Sub
The error i run into is the object doesnt support this property or method.
i am using a for loop in this case because i want to select all the options that are available.
What am i doing wrong here?
Thanks in advance

Sub extract()
Dim oIE As Object
Dim ohtml As HTMLDocument
Dim objOption As Object
Set oIE = CreateObject("InternetExplorer.Application")
oIE.Navigate ("http://itbidfloors.transcanada.com/")
oIE.Visible = True
' Wait while IE loading
Do
DoEvents
Loop Until oIE.ReadyState = 4
For Each objOption In oIE.Document.getElementsByClassName("locationSelect").getElementsByID("ReceiptLocations").Options
objOption.Selected = True
Next
End Sub

Related

VBA Select from dropdown list while filling webform

I'm quite new to VBA and HTML. I try to write a VBA macro which fill in a web form.
I am struck at a point where I have to select value from the dropdown list.
Sub test()
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.Navigate ("https://www.fakturowo.pl/wystaw")
Do While IE.ReadyState <> READYSTATE_COMPLETE
Loop
Set doc = IE.Document
doc.getElementById("rodzaj").Value = 26
doc.getElementById("miasto").Value = "XYZ"
doc.getElementById("nazwa_sprzedawca").Value = "XYZ"
doc.getElementById("ulica_sprzedawca").Value = "XYZ"
Here is the HTML:
<select name='rodzaj' id="rodzaj" onchange="javascript:submit();">
<option value='47'>Dobowy raport fiskalny</option>
<option value='24'>Dowód dostawy</option>
<option value='27'>Dowód dostawy (tylko ilość)</option>
<option value='7'>Dowód wewnętrzny</option>
<option value='42'>Eksport towarów</option>
<option value='48'>Eksport usług</option>
<option value='49'>Eksport usług (VAT-EU 28b)</option>
<option value='62'>Faktura</option>
<option value='21'>Faktura (bez vat, dawny rachunek)</option>
<option value='45'>Faktura (odwrotne obciążenie bez vat)</option>
<option value='35'>Faktura (odwrotne obciążenie z vat)</option>
<option value='69'>Faktura MPP (mechanizm podzielonej płatności)</option>
<option value='0' selected>Faktura VAT</option>
<option value='44'>Faktura VAT MP</option>
<option value='26'>Faktura VAT RR</option>
<option value='5'>Faktura VAT marża</option>
Many thanks!
Gosia
You must trigger the html change event of the dropdown. To do that you must save the html dropdown element do an object variable. So you can give it to the needed function to trigger html events.
I have commented the relevant code parts:
Sub test()
Dim IE As Object
Dim nodeDropDown As Object 'Needed to set the dropdown as DOM Object
Dim doc As Object
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.Navigate ("https://www.fakturowo.pl/wystaw")
Do While IE.ReadyState <> 4: DoEvents: Loop
Set doc = IE.Document
'set the dropdown as DOM object
Set nodeDropDown = doc.getElementById("rodzaj")
'Change the value of the dropdown
nodeDropDown.Value = 26
'Trigger the html change event of the dropdown
Call TriggerEvent(doc, nodeDropDown, "change")
'Wait to load the page new after changing the dropdown value
Application.Wait (Now + TimeSerial(0, 0, 5))
doc.getElementById("miasto").Value = "XYZ"
doc.getElementById("nazwa_sprzedawca").Value = "XYZ"
doc.getElementById("ulica_sprzedawca").Value = "XYZ"
End Sub
This is the procedure to trigger a html event
Private Sub TriggerEvent(htmlDocument As Object, htmlElementWithEvent As Object, eventType As String)
Dim theEvent As Object
htmlElementWithEvent.Focus
Set theEvent = htmlDocument.createEvent("HTMLEvents")
theEvent.initEvent eventType, True, False
htmlElementWithEvent.dispatchEvent theEvent
End Sub

Input date on date field of a website and click on view report button

I've used VBA but never for scraping website so I'm a beginner.
What I'm trying is to use VBA to go to my SSRS website from work, input the date for the report and run it. I've tried my best to code it but it keeps giving me
Run Error Time 91: Object Variable or With block Variable not set.
Below is my code
Sub AgingReport()
Dim IE As New SHDocVw.InternetExplorerMedium
Dim HTMLDoc As MSHTML.HTMLDocument
Dim ReportDate As MSHTML.IHTMLElement
IE.navigate "www.example.com"
IE.navigate
IE.Visible = True
Application.Wait Now + #12:00:04 AM#
Do While IE.readyState <> READYSTATE_COMPLETE
Loop
Set HTMLDoc = IE.document
Set ReportDate = HTMLDoc.getElementById("ReportViewerControl_ctl04_ctl09_txtValue")
ReportDate.Value = "06/28/2019"
HTMLDoc.getElementById("ReportViewerControl_ctl04_ctl00").Click
End Sub
Below is the HTML. Please let me know how I can approve the code.
HTML For Date Field
<input name="ReportViewerControl$ctl04$ctl09$txtValue" class="null"
id="ReportViewerControl_ctl04_ctl09_txtValue" onkeypress="if
(WebForm_TextBoxKeyHandler(event) == false) return false;" onchange="javascript:setTimeout('__doPostBack(\'ReportViewerControl$ctl04$ctl09$txtValue\',\'\')', 0)" type="text" size="28">
Here's the HTML for the the button that is code as input
<input name="ReportViewerControl$ctl04$ctl00" id="ReportViewerControl_ctl04_ctl00" type="submit" value="View Report">
I have changed some of your code and added one HTML Button Element
Hope it helps!
Sub AgingReport()
Dim IE As New SHDocVw.InternetExplorerMedium
Dim HTMLDoc As MSHTML.HTMLDocument
Dim ReportDate As MSHTML.IHTMLElement
Dim HtmlButton As MSHTML.IHTMLElement
IE.navigate "www.example.com"
IE.navigate
IE.Visible = True
Application.Wait Now + #12:00:04 AM#
Do While IE.readyState <> READYSTATE_COMPLETE
Loop
Set HTMLDoc = IE.document
Set ReportDate = HTMLDoc.getElementById("ReportViewerControl_ctl04_ctl09_txtValue")
ReportDate.Value = "06/28/2019"
Set HtmlButton = HTMLDoc.getElementById("ReportViewerControl_ctl04_ctl00")
HtmlButton.Click
End Sub

how to select an option from a dropdown list in a webpage using the inner text

How to select an option from a dropdown list in a webpage using the inner text
below is vba code
Sub navigation_site_with_part_number()
Dim eRow As Long
Dim ele As Object
Set sht = Sheets("Sheet1")
RowCount = 1`enter code here
eRow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
Set objIE = CreateObject("InternetExplorer.Application")
requestnumber = InputBox("Enter the request number.")
With objIE
.Visible = True
.navigate "*http://www.auto.com*"
Do While .Busy Or _
.readyState <> 4
DoEvents
Loop
.document.getElementsByid("inspNumber").selectedinnertext = 2
.document.getElementById("InspQuery_Search").Click
Do While .Busy Or _
.readyState <> 4
DoEvents
Loop
End With
Below is Html code:
div id="RequestNumberGroup" class="form-group">
<label for="TextRequestNumber">Request Number:</label>
<select class="form-control" id="inspNumber" name="inspNumber">
<option value=""></option>
<option value="813">20190205</option>
<option value="1034">20190426</option>
<option value="1059">20190451</option>
<option value="1061">20190453</option>
<option value="1064">20190456</option>
<option value="1065">20190457</option>
I expected to select the option in dropdownlist with requestnumber not option value
There isn't a select by text option in IE implementation.
1) Either gather collection/nodelist of options and loop comparing innerText against desired text string then select if found and Exit For
Dim options As Object, i As Long, found As Boolean
Set options = ie.document.querySelectorAll("#inspNumber options")
For i = 0 To options.length - 1
If options.item(i).innerText = "target string" Then
options.item(i).Selected = True
found = True
Exit For
End If
Next
If Not found Then
'do something if not found
End If
2) Use selenium vba wrapper which does implement this functionality
Dim ele As Select
Set ele = driver.FindElementById("inspNumber").AsSelect
ele.SelectByText "Option Text"
N.B. Of course there are using attributes values and selectedIndex methods but you didn't want those.

Object Variable or With Block Variable not set getelementsbyname vba

I am trying to pull the data from websites. So I want to select 3 drop-down values in below URL but i cant change those values. example i want to select month
<select name="fmonth1" id="fmonth1" class="dropdownboxlang" size="1" style="width:60px;">
<option value="0">MM</option>
<option value="1">Jan</option>
<option value="2">Feb</option>
<option value="3">Mar</option>
<option value="4">Apr</option>
<option value="5">May</option>
<option value="6">Jun</option>
<option value="7">Jul</option>
<option value="8">Aug</option>
<option value="9">Sep</option>
<option value="10">Oct</option>
<option value="11">Nov</option>
<option value="12">Dec</option>
</select>
the error i am getting while changing the value in drop-down. I am trying all the possible ways from two days. Any suggestion would be appreciated.
Public Sub bse()
Dim IE As InternetExplorer
Dim HTML As HTMLDocument
Dim Dropdown As IHTMLElement
Dim dropOption As IHTMLElement
Set IE = CreateObject("InternetExplorer.Application")
With IE
.Visible = True
.Navigate "https://www.bseindia.com/markets/debt/BhavCopyDebt.aspx?expandable=6"
End With
Do
DoEvents
Application.Wait Now() + TimeValue("00:00:01")
Loop Until IE.ReadyState = 4 And Not IE.Busy
Set HTML = IE.Document
HTML.getElementsByName("fmonth1")(0).Value = "1" error line
IE.Quit
End Sub
You can use css id selector in descendant combination with attribute = value selector
#fmonth1 option[value='1']
That is:
ie.document.querySelector("#fmonth1 option[value='1']")
You may need .Click on the end for selected. Can't test that url but also try:
ie.document.querySelector("#fmonth1 option[value='1']").Selected = True
More generally, if you know an element exists, and your syntax is correct, but you are still getting not set then it may be a timing issue where you need a longer wait (e.g. timed loop) before attempting to access, e.g.
Const MAX_WAIT_SEC As Long = 10
Dim t As Date, ele As Object
t = Timer
Do
DoEvents
On Error Resume Next
Set ele = ie.document.querySelector("#fmonth1")
On Error GoTo 0
If Timer - t > MAX_WAIT_SEC Then Exit Do
Loop While ele Is Nothing
If Not ele Is Nothing Then
ele.Click 'may be needed to expose options
ie.document.querySelector("#fmonth1 option[value='1']").Selected = True
End If
If inside of a parent iframe/frame you will need to navigate that first e.g.
ie.document.getElementsByTagName("iframe")(0).contentDocument.querySelector("#fmonth1 option[value='1']").Selected = True
I tested the following now I can access the page:
Public Sub MakeSelection()
Dim ie As New InternetExplorer
With ie
.Visible = True
.navigate "https://www.bseindia.com/markets/debt/BhavCopyDebt.aspx?expandable=6"
While .Busy Or .readyState < 4: DoEvents: Wend
.document.getElementsByTagName("iframe")(0).contentDocument.querySelector("#fmonth1 option[value='1']").Selected = True
Stop '<==Delete me later
.Quit
End With
End Sub

How do you reference the HTMLSelect object from VBA?

I am trying to reference the HTMLSelect object in VBA. Assuming I have a form on the html page and a select control named LB. I can't figure out why I get an error message using the code below.
Dim ieApp As InternetExplorer
Dim ieSelect As HTMLSelect
Dim iePage as HTMLDocument
Set ieApp = New InternetExplorer
ieApp.Visible = True
ieApp.navigate "C:\formA.html"
Do Until ieApp.readyState = READYSTATE_COMPLETE
Loop
Application.Wait (Now() + TimeValue("00:00:05"))
Set iePage = ieApp.document
Set ieSelect = iePage.forms(0).Item("LB")
Hm, not sure, but have you the runat="server" on the in formA.html?