How to select values from Dropdown menu on a webpage:VBA - vba

My code :
Sub login()
Dim IE As Object
Dim HTMLDoc As Object, HTMLDoc2 As Object
Dim objCollection As Object
Const navOpenInNewTab = &H800
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.Navigate "https:/com/1/19/login.esp"
Do While IE.Busy Or IE.ReadyState <> 4: Loop
Set HTMLDoc = IE.Document
With HTMLDoc
HTMLDoc.getElementById("USERNAME").Value = "xxxx" 'Entering credential
HTMLDoc.getElementById("PASSWORD").Value = "yyyyy"
End With
Set objCollection = IE.Document.getElementById("loginbutton")
objCollection.Click
'Second webpage
Do While IE.Busy Or IE.ReadyState <> 4: Loop ' opening the second webpage
Set HTMLDoc2 = IE.Document
With HTMLDoc2
**HTMLDoc2.getElementById("DEPARTMENTID").selectedindex = 1 'Drop down menu
HTMLDoc2.getElementById("DEPARTMENTID").FireEvent ("onchange")**
End With
Set objCollection = IE.Document.getElementById("loginbutton")
objCollection.Click
End Sub
Q)What code changes do I do to select Dwell_DF option Value 1567?
The above code gives run time error '424' : Object required.
HTMLDoc2.getElementById("DEPARTMENTID").selectedindex = 1 'Drop down menu
HTMLDoc2.getElementById("DEPARTMENTID").FireEvent ("onchange")
The above line give the error.
In the first webpage I fill the login credentials then in the next page is that of the image pasted with this post. Here I want to change the value in the drop down menu.

Give this a try. The value "1567" corresponds with the InnerText "Dwell_DF".
With HTMLDoc2
.getElementById("DEPARTMENTID").Focus
.getElementById("DEPARTMENTID").Value = "1567" 'You can also loop to find the text of the Option
.getElementById("DEPARTMENTID").FireEvent ("onchange")
End With

It should be something like this.
Sub passValueToComboBox1()
Dim ie As Object
Dim oHTML_Element As IHTMLElement
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
ie.navigate "http://your_URL_here.php"
While ie.Busy Or ie.readyState <> 4: DoEvents: Wend
Set oHTML_Element = ie.document.getElementsByName("selectedReportClass")(0)
If Not oHTML_Element Is Nothing Then oHTML_Element.Value = "FUBU7"
For Each oHTML_Element In ie.document.getElementsByTagName("input")
If oHTML_Element.Type = "submit" Then oHTML_Element.Click: Exit For
Next
End Sub
Check out the link below for some other ideas of how to programatically interact with a web site.
http://vbadud.blogspot.com/2009/08/how-to-login-to-website-using-vba.html

Related

VBA: click on next button on a website (loop)

I'm trying to get info from a website with many pages. I already got the extraction of the information, however I can't perform a loop to click the "next page" button and do it again..
Can you please help me?
Thanks,
M
Sub website_test()
Dim ie As Object
Dim ht As HTMLDocument
Dim button As Object
Dim i As Integer
For i = 0 To 100
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
ie.navigate ("https://old.reddit.com/r/fashion/new/")
Do Until ie.readyState = 4
DoEvents
Loop
Set ht = ie.document
Set elems = ht.getElementsByClassName("title may-blank")
For Each elem In elems
Debug.Print (elem.innerText)
Next
Set button = ht.getElementsByTagName("nofollow next")
button(i + 1).Click --> **is this where it gets an error (run-time error 91: object variable or width block variable not set)**
Next i
End Sub
I mainly modified the way you get the 'next' button Element from DOM.
Sub website_test()
Dim ie As Object
Dim ht As HTMLDocument
Dim button As Object
Dim i As Integer
For i = 0 To 5
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
ie.navigate ("https://old.reddit.com/r/fashion/new/")
Do Until ie.readyState = 4
DoEvents
Loop
Set ht = ie.document
Set elems = ht.getElementsByClassName("title may-blank")
For Each elem In elems
Debug.Print (elem.innerText)
Next
Set button = ht.querySelector("[rel='nofollow next']")
button.Click
ie.Quit
Next i
End Sub

Clicking a button in Internet Explorer

I am trying to create some VBA code that clicks a button with the following HTML:
<button class="button small btnViewAction atlas--ui-button" type="button" data-tracking-value="Action" data-tracking-label="View Action" data-tracking-## Heading ##category="Reconciliations" data-attribs-childassignmentid="661" data-attribs-reconciliationid="145870" data-attribs-assignmenttype="A" data-attribs-assignmentid="1223" value="undefined" data-columnname="action">Edit</button>
Is there a way to reference this button?
Without seeing more of the HTML I can't say this will work for sure, but it should give you a good guideline for what you can do!
Make sure you update ie.navigate to your site.
This is a late binding example, you can also set a reference to Microsoft Internet Controls.
Sub clickButton()
Dim ie As Object
Dim Btn As Object
'Late Binding
Set ie = CreateObject("InternetExplorer.Application")
On Error GoTo Catch
ie.Visible = True
ie.navigate "https://yourwebsite.com/"
While ie.ReadyState <> 4 Or ie.Busy: DoEvents: Wend
'LOOP EACH CLASS ELEMENT
For Each Btn In ie.Document.getElementsByClassName("button")
If Btn.innertext = "Edit" Then
Btn.Click
End If
Next Btn
'CLOSE INSTANCE OF IE
Catch:
ie.Quit
Set ie = Nothing
End Sub
Here is a very generic sample of how to do this.
Dim HTMLDoc As HTMLDocument
Dim oBrowser As InternetExplorer
Sub Login_2_Website()
Dim oHTML_Element As IHTMLElement
Dim sURL As String
On Error GoTo Err_Clear
sURL = "https://www.google.com/accounts/Login"
Set oBrowser = New InternetExplorer
oBrowser.Silent = True
oBrowser.timeout = 60
oBrowser.navigate sURL
oBrowser.Visible = True
Do
' Wait till the Browser is loaded
Loop Until oBrowser.readyState = READYSTATE_COMPLETE
Set HTMLDoc = oBrowser.Document
HTMLDoc.all.Email.Value = "sample#vbadud.com"
HTMLDoc.all.passwd.Value = "*****"
For Each oHTML_Element In HTMLDoc.getElementsByTagName("input")
If oHTML_Element.Type = "submit" Then oHTML_Element.Click: Exit For
Next
' oBrowser.Refresh ' Refresh If Needed
Err_Clear:
If Err <> 0 Then
Debug.Assert Err = 0
Err.Clear
Resume Next
End If
End Sub
Try the code above and see if you can implement it for your specific situation. If you need a nudge, post back, with additional requirements.

Selecting value from a dropdown list on a webpage using vba

On this site I am able to select the country and language from dropdown menu but when I click on "Complete new application form" button. It says fields are empty.
Any help would be appreciated.
Sub Test()
strURL = "https://visa.kdmid.ru/PetitionChoice.aspx"
With ie
.Visible = True
.navigate strURL
While .Busy
DoEvents
Wend
Set html = .document
'Country where you will apply for visa.
Set ctY = html.getElementById("ctl00$phBody$Country")
For i = 1 To ctY.Options.Length
If ctY.Options(i).Text = "NETHERLANDS" Then
ctY.selectedIndex = i
Exit For
End If
Next i
'Select Language
Set lnG = html.getElementById("ctl00$phBody$ddlLanguage")
For i = 1 To lnG.Options.Length
If lnG.Options(i).Text = "ENGLISH" Then
lnG.selectedIndex = i
Exit For
End If
Next i
'Click I have read instructions check box
html.getElementById("ctl00$phBody$cbConfirm").Click
'Click apply button
Set btnGo = html.forms(0).all("ctl00$phBody$btnNewApplication")
btnGo.Click
End With
End Sub
So you are on the right track but if you look at the HTML of the site there are actually two elements with the country selection- you got the first one, 'ctl00_phBody_Country', but this is actually just the drop down, and the actual selected value is stored in 'ctl00_phBody_cddCountry_ClientState'... the language section has similar structure. Lastly the accepted value is not just the country name you see in the drop down, it is actually a combination of a country code from the drop down and the country name....
See below for sample code:
Public Sub Test()
Dim IE As InternetExplorer
Dim HTMLDoc As HTMLDocument
Dim countryStr As String
Dim countryObj As HTMLObjectElement
Dim countryCodes As IHTMLElementCollection
Dim codeCounter As Long
Dim languageStr As String
Dim languageObj As HTMLObjectElement
Dim languageCodes As IHTMLElementCollection
countryStr = "Netherlands"
languageStr = "English"
Set IE = New InternetExplorer
With IE
.Visible = False
.Navigate "https://visa.kdmid.ru/PetitionChoice.aspx?AspxAutoDetectCookieSupport=1"
While .Busy Or .ReadyState <> READYSTATE_COMPLETE: Wend
Set HTMLDoc = IE.document
End With
Set countryObj = HTMLDoc.getElementById("ctl00_phBody_cddCountry_ClientState")
Set countryCodes = HTMLDoc.getElementById("ctl00_phBody_Country").getElementsByTagName("option")
For codeCounter = 0 To countryCodes.Length - 1
If countryCodes(codeCounter).innerText = UCase(countryStr) Then
countryObj.Value = countryCodes(codeCounter).Value & ":::" & countryCodes(codeCounter).innerText & ":::"
While IE.Busy Or IE.ReadyState <> READYSTATE_COMPLETE: Wend
Exit For
End If
Next
Set languageObj = HTMLDoc.getElementById("ctl00_phBody_cddLanguage_ClientState")
Set languageCodes = HTMLDoc.getElementById("ctl00_phBody_ddlLanguage").getElementsByTagName("option")
For codeCounter = 0 To languageCodes.Length - 1
If languageCodes(codeCounter).innerText = UCase(languageStr) Then
languageObj.Value = languageCodes(codeCounter).Value & ":::" & languageCodes(codeCounter).innerText & ":::"
While IE.Busy Or IE.ReadyState <> READYSTATE_COMPLETE: Wend
Exit For
End If
Next
HTMLDoc.getElementById("ctl00$phBody$cbConfirm").Click
While IE.Busy Or IE.ReadyState <> READYSTATE_COMPLETE: Wend
HTMLDoc.getElementById("ctl00_phBody_btnNewApplication").Click 'Launch Form
IE.Quit
Set IE = Nothing
End Sub

Use VBA IE Automation to send message from pinkoi.com

Forgive my English. Using VBA Automation of Internet Explorer, I want to send message from pinkoi.com.
Now I can show the send message form,But I don't know how to input the subject and body and select file and send it.
the send form photo
Sub test()
Dim ie As Object 'SHDocVw.InternetExplorer
Set ie = CreateObject("InternetExplorer.Application")
Call login(ie) 'just for login
Call show_the_form(ie) 'My problem in here
End Sub
Sub login(ie)
ie.Visible = True
ie.Navigate "https://www.pinkoi.com/user/testpkko"
Do While ie.Busy Or ie.readyState <> 4: DoEvents: Loop
Dim anchorTags As Object 'MSHTML.IHTMLElementCollection
Set anchorTags = ie.document.GetElementsByTagName("A")
Dim oAnchorLoop As Object 'MSHTML.IHTMLAnchorElement
For Each oAnchorLoop In anchorTags
Dim anchorText As String
anchorText = oAnchorLoop.Text
If anchorText = ChrW(20659) & ChrW(36865) & ChrW(35338) & ChrW(24687) Then
Dim oAnchorLogon As Object 'MSHTML.IHTMLAnchorElement2
Set oAnchorLogon = oAnchorLoop
oAnchorLogon.Click
Exit For
End If
Next
Do While ie.Busy Or ie.readyState <> 4: DoEvents: Loop
'* so now logon form should be visible
On Error Resume Next
Dim oUserNameInput As Object 'MSHTML.IHTMLInputElement
Set oUserNameInput = ie.document.getElementById("n-login-id")
nowtime = Timer
Do While ie.Busy Or ie.readyState <> 4 Or oUserNameInput Is Nothing
DoEvents
If Timer - nowtime > 1 Then Exit Do 'Already logoin
Set oUserNameInput = ie.document.getElementById("n-login-id")
Loop
oUserNameInput.Value = "testpkko"
Dim oUserPassword As Object 'MSHTML.IHTMLInputElement
Set oUserPassword = ie.document.getElementById("n-login-password")
oUserPassword.Value = "abc123"
Dim oListElement As Object 'MSHTML.HTMLLIElement
Set oListElement = oUserNameInput.parentElement
Dim oUnorderedList As Object 'MSHTML.IHTMLUListElement
Set oUnorderedList = oListElement.parentElement
Dim oForm As Object 'MSHTML.IHTMLFormElement
Set oForm = oUnorderedList.parentElement
Dim oSubmitInputElememt As Object 'MSHTML.HTMLInputElement
Set oSubmitInputElememt = Nothing
Dim lFormChildrenLoop As Long
For lFormChildrenLoop = 1 To oForm.all.Length
If oForm.elements.Item(lFormChildrenLoop).Type = "submit" Then
Set oSubmitInputElememt = oForm.elements.Item(lFormChildrenLoop)
Exit For
End If
Next lFormChildrenLoop
If Not oSubmitInputElememt Is Nothing Then
'Stop '* get ready .....
oSubmitInputElememt.Click
Do While ie.Busy Or ie.readyState <> 4: DoEvents: Loop
End If
End Sub
Sub show_the_form(ie)
Application.Wait Now + TimeValue("00:00:03")
ie.Navigate "https://en.pinkoi.com/user/testpkko2" 'After login we can trip to other people website
Do While ie.Busy Or ie.readyState <> 4: DoEvents: Loop
With ie.document
On Error Resume Next
For Each E In .GetElementsByTagName("A")
a = E.Text
If CStr(a) = "Message" Then
E.Click 'After Click I want to send a message
'how can I input Subject and body and select file to upload?
'And I want to learn how to do this , Very thanks.
End If
Next
End With
End Sub
Sorry we downvoted you, try this. If we click on the anchor you specified but I needed to use Unicode character logic. The rest is just navigating the DOM. I have put default username and password of "hello" and "world".
Option Explicit
Private Sub test()
'use ie open https://www.pinkoi.com/
'Please use FB login then web
'* Might want to consider using the Tools References to give yourself Intellisense
'* Tools->References Microsoft Internet Controls
'* Tools->References Microsoft HTML Object Library
Dim IE As Object 'SHDocVw.InternetExplorer
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.Navigate "https://www.pinkoi.com/user/testpkko"
Do While IE.Busy Or IE.readyState <> 4: DoEvents: Loop
Dim anchorTags As Object 'MSHTML.IHTMLElementCollection
Set anchorTags = IE.document.GetElementsByTagName("A")
Dim oAnchorLoop As Object 'MSHTML.IHTMLAnchorElement
For Each oAnchorLoop In anchorTags
Dim anchorText As String
anchorText = oAnchorLoop.Text
If anchorText = ChrW(20659) & ChrW(36865) & ChrW(35338) & ChrW(24687) Then
Dim oAnchorLogon As Object 'MSHTML.IHTMLAnchorElement2
Set oAnchorLogon = oAnchorLoop
oAnchorLogon.Click
Exit For
'oAnchorLoop.Click 'here can show the form,but i can put message and select file from it
End If
Next
Do While IE.Busy Or IE.readyState <> 4: DoEvents: Loop
'* so now logon form should be visible
On Error Resume Next
Dim oUserNameInput As Object 'MSHTML.IHTMLInputElement
Set oUserNameInput = IE.document.getElementById("n-login-id")
Do While IE.Busy Or IE.readyState <> 4 Or oUserNameInput Is Nothing
DoEvents
Set oUserNameInput = IE.document.getElementById("n-login-id")
Loop
oUserNameInput.Value = "hello"
Dim oUserPassword As Object 'MSHTML.IHTMLInputElement
Set oUserPassword = IE.document.getElementById("n-login-password")
oUserPassword.Value = "world"
Dim oListElement As Object 'MSHTML.HTMLLIElement
Set oListElement = oUserNameInput.parentElement
Dim oUnorderedList As Object 'MSHTML.IHTMLUListElement
Set oUnorderedList = oListElement.parentElement
Dim oForm As Object 'MSHTML.IHTMLFormElement
Set oForm = oUnorderedList.parentElement
Dim oSubmitInputElememt As Object 'MSHTML.HTMLInputElement
Set oSubmitInputElememt = Nothing
Dim lFormChildrenLoop As Long
For lFormChildrenLoop = 1 To oForm.all.Length
If oForm.elements.Item(lFormChildrenLoop).Type = "submit" Then
Set oSubmitInputElememt = oForm.elements.Item(lFormChildrenLoop)
Exit For
End If
Next lFormChildrenLoop
If Not oSubmitInputElememt Is Nothing Then
Stop '* get ready .....
oSubmitInputElememt.Click
End If
End Sub
I alreay know how to input subject and body and send the file
ie.document.all("title").Value = "subject"
ie.document.all("description").Value = "Body"
For Each e In ie.document.GetElementsByTagName("INPUT")
a = e.Type
If CStr(a) = "submit" Then e.Click
Next
But after click select file , it will no response and program will stop
did anybody can use vba to select file?
ie.document.all("file").Click

How to automatically fill value in a box in webpage using via excel

This is not for the first time I am mentioning this...but yeah I am a rookie in vba and in need of help...
I am having trouble in filling up a value in a box in webpage using vba excel.
The source code of the box is
<input
name="MultiRenameTable_OR:wt.epm.EPMDocument:3053059700_NEWNAME_JSID"
class="enabledfield"
id="MultiRenameTable_OR:wt.epm.EPMDocument:3053059700_NEWNAME_JSID"
style="WIDTH:98% onblur="setJSAttributeValue(this,'NEWNAME_JSID','MultiRenameTable', 'epmdoc_09876889_u46_drw',undefined)" type="text" size="25" ?=""
trlId="MultiRenameTable_epmdoc_09876889_u46_drw_NEWNAME_JSID"></input>
the code i wrote is
Sub xx()
Dim ie As Object
Dim doc As HTMLDocument
Dim lo As IHTMLElementCollection
Dim l As IHTMLElement
Set ie = CreateObject("internetexplorer.application")
ie.navigate "mysiteurl"
ie.Visible = True
Do While ie.Busy: DoEvents: Loop
Do While ie.readyState <> 4: DoEvents: Loop
Set doc = ie.document
doc.getElementById("MultiRenameTable_OR:wt.epm.EPMDocument3053059700_NEWNAME_JSID").Value = "xyz"
End Sub
It doesnt set the value.
Also i cannot use "name","ID","trlID","onblur" because they keep on changing each time i launch IE.
Please help me with this.
I'm guessing from the setJSAttributeValue that the name will always contain the substring "NEWNAME_JSID". Just grab all of the input elements, then loop through them looking for it in the name:
Sub xx()
Dim ie As Object
Dim doc As HTMLDocument
Dim lo As IHTMLElementCollection
Dim l As IHTMLElement
Set ie = CreateObject("internetexplorer.application")
ie.navigate "mysiteurl"
ie.Visible = True
Do While ie.Busy: DoEvents: Loop
Do While ie.readyState <> 4: DoEvents: Loop
Set doc = ie.document
Set lo = doc.getElementsByTagName("input")
For Each l In lo
If l.Name Like "*NEWNAME_JSID*" Then
'Do whatever.
Exit For
End If
Next l
End Sub