Excel VBA automate data entry in a web - excel

Sub Godmode()
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.navigate "http://charterweb.santanderuk.gs.corp/CharterServiceChain/default.aspx"
Do While IE.Busy
Application.Wait DateAdd("s", 1, Now)
Loop
IE.document.getElementById("M_E_ctlPartField208_V").Value = "test"
End Sub
I am always getting an error saying; method document of object iwebbrowser2 failed

You need to get the element ID, then as a second command assign a value to that element.
elementIwantToChange = IE.document.getElementById("M_E_ctlPartField208_V")
elementIwantToChange.Value = "test"
Also you should test whether the element ID returned is "null"

Related

Unable to autofill tabs of website through excel vba

I have managed to create the below code, but I'm unable to autofill the fields marked with red arrows shown in the image, as there is no ID available for them in the HTML code.
All the tabs shown in image does not have IDs.
What should be the coding for filling these ?
Sub TDS_Autofill()
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.navigate "https://onlineservices.tin.egov-nsdl.com/etaxnew/tdsnontds.jsp"
Do While IE.Busy
Application.Wait DateAdd("s", 1, Now)
Loop
Set doc = IE.document
doc.parentWindow.execScript "sendRequest(281)", "JavaScript"
Do While IE.Busy
Application.Wait DateAdd("s", 1, Now)
Loop
If ThisWorkbook.Sheets("Challan AutoFill").Range("m2").Value = "Company"
Then
doc.getElementById("0020").Click
ElseIf ThisWorkbook.Sheets("Challan AutoFill").Range("m2").Value = "Non
Company" Then
doc.getElementById("0021").Click
End If
If ThisWorkbook.Sheets("Challan AutoFill").Range("o2").Value = "(200)
TDS/TCS Payable by Taxpayer" Then
doc.getElementById("200").Click
ElseIf ThisWorkbook.Sheets("Challan AutoFill").Range("o2").Value = "(400)
TDS/TCS Regular Assessment" Then
doc.getElementById("400").Click
End If
IE.document.querySelector("select.form-control").selectedIndex =
ThisWorkbook.Sheets("Challan AutoFill").Range("r2").Value
doc.getElementById("NetBanking").Click
doc.getElementById("NetBank_Name_c").Value = ThisWorkbook.Sheets("Challan
AutoFill").Range("t2").Value
End Sub```
You could get the inputs using their names by doc.getElementsByName. It returns a collection of all elements in the document with the specified name. Then you should find out the index number of the element you want.
For example, you can set the value of "Tax Deduction Account No" using the following code:
doc.getElementsByName("TAN")(1).Value = "abc"
For the other inputs, you could use the same methods. I make a test with setting some of the values and you can see the result:
You can use getElementsByTag("input") that returns a collection of components that you can access with an index, and then set their value to what you need.
doc.getElementsByTagName("input")(0).value = "My value"

Automating a Report: Selecting from a drop down in IE using VBA

I'm trying to automate a report of mine using VBA. I was able to do the log in part and all but when it comes to navigating the next webpage with drop downs I can't seem to get a hold of it. I tried multiple ways to fill out the first drop down which is the report type and I can't find the correct code
All commented "'" are all the codes I've tried. I can't post the website since it's a client website.
html "inspect elemet" photo
Sub Get_RawFile()
Dim IE As New InternetExplorer
Dim HTMLDoc As HTMLDocument
Dim addressInput As HTMLInputElement
With IE
.Visible = True
.Navigate ("------------------------")
While IE.Busy Or IE.readyState <> 4: DoEvents: Wend
Set HTMLDoc = IE.document
HTMLDoc.all.UserName.Value = Sheets("Data Dump").Range("A1").Value
HTMLDoc.all.Password.Value = Sheets("Data Dump").Range("B1").Value
HTMLDoc.getElementById("login-btn").Click
While IE.Busy Or IE.readyState <> 4: DoEvents: Wend
'HTMLDoc.getElementByName("ddlReportType")(0).Value = "1"
'Set HTMLDoc = IE.document
'Set evtChange = HTMLDoc.createEvent("HTMLEvents")
'evtChange.initEvent "change", True, False
'Set selectElement = HTMLDoc.getElementById("ddlReportType")
'selectElement.Value = "1" 'Attendance
'selectElement.dispatchEvent evtChange
'Set htmlSelectElem = HTMLDoc.getElementsByTagName("ddlReportType")
'htmlSelectElem.selectedIndex = 1
'Set reporttype = IE.document.getElementById("ddlReportType")
'For i = 1 To reporttype.Options.Length
'If reporttype.Options(i).Text = "Attendance" Then
'reporttype.selectedIndex = i
'Exit For
'End If
'Next i
End With
Run-time error '438'
Object doesn't support this property or method
This method .getElementByName does not exist. It should be .getElementsByName("Something"), in which case the method returns a collection of elements whose Name attribute is Something.
The following would access the first element of the collection of elements whose Name attribute is ddlReportType and set its value to 1:
HTMLDoc.getElementsByName("ddlReportType")(0).Value = "1"
Having said that, you don't seem so sure as to what ddlReportType is.
If it's the id of an element then the element should look like so:
<tagName id="ddlReportType">Something</tagName>
In that case you should use the method .getElementByID()
If it's the Name of an element then the element should look like so:
<tagName Name="ddlReportType">Something</tagName>
In that case you should use the method .getElementsByName(), which returns a collection of elements.
If it's the Class of an element then the element should look like so:
<tagName Class="ddlReportType">Something</tagName>
In that case you should use the method .getElementsByClassName(), which returns a collection of elements.
Finally, I can tell you for sure that it's not a Tag Name so you definitely shouldn't use the .getElementsByTagName method.
EDIT
From the image you provided after editing your original post, it is clear that ddlReportType is the id of a <select></select> element. So you should do it this way:
Dim HTMLselect As HTMLSelectElement
Set HTMLselect = HTMLDoc.getElementById("ddlReportType")
HTMLselect.Value = "1"
References used: Microsoft HTML Object Library (VBE>Tools>References>...)
That's how to select one of the drop down options. To navigate further you would have to press some button I presume.

getElementById won't work in VBA, error 438

I am relatively new at VBA (I know the basics, but not much else) and I am trying to get some code set up that will fill out online forms for me, but when I run my code I get a 438 error:
object doesnt support this property or method
when it gets to
ie.document.getElementById ("q")
I have added the HTML object library and Microsoft internet controls to my references. I have looked at tons of online forums. I have even copied and pasted an entire script directly into VBA. Nothing will make it use getElementById(). Here is my code:
Sub internetstuff()
Dim ie As Object
Set ie = CreateObject("internetexplorer.application")
ie.Visible = True
ie.navigate ("https://www.google.com/")
Set searchbx = ie.document.getElementById("q")
searchbx.Value = "Howdy!"
End Sub
What should happen is that it should open InternetExplorer, go to Google, and populate the search bar with "Howdy!".
Instead, It only opens google, then I get the error message and it stops running.
Because "q" is not an ID, it's a Name.
You have to use GetElementsByName and select the 1st Element
try this:
Sub internetstuff()
Dim ie As Object
Set ie = CreateObject("internetexplorer.application")
ie.Visible = True
ie.Navigate ("https://www.google.com/")
Do Until ie.ReadyState >= 4
DoEvents
Loop
Set searchbx = ie.document.getElementsByName("q")(0)
searchbx.Value = "Howdy!"
End Sub
I have also added a waiting Event, in case it takes time to load Google.
Result:
A more concise and efficient way is to use querySelector to return first match for the name attribute
Option Explicit
Public Sub internetstuff()
Dim ie As Object
Set ie = CreateObject("InternetExplorer.Application")
With ie
.Visible = True
.Navigate2 "https://www.google.com/"
While .Busy Or .readyState < 4: DoEvents: Wend
.document.querySelector("[name=q]").Value = "Howdy"
Stop '<delete me later
.Quit
End With
End Sub

Cannot perform auto search on a specific website

Recently I am learning to use excel macro to search on a website. I've read several forum threads and I came up with the code below. However, error appears when I reach the row
SearchBox(0).Value = SearchString
I tried to remove the (0) but another error appears as well. The code works well on other websites. How should I change them to adapt to this site?
P.S. I would also like to know the way to click the search button.
Sub Searchstockcode()
Dim SearchString As String
SearchString = "700"
Set ie = CreateObject("InternetExplorer.Application")
With ie
ie.Visible = True
End With
ie.Navigate "http://www.hkexnews.hk/listedco/listconews/advancedsearch/search_active_main.aspx"
While ie.ReadyState <> 4
DoEvents
Wend
Dim SearchBox As Object
Set SearchBox = ie.Document.GetElementsByName("ct100$txt_stock_code")
SearchBox(0).Value = SearchString
Dim SearchButton As Object
Set SearchButton = ie.Document.GetElementsByName
End Sub
I don't know whether the problem with your name selection is due to the element having two name attributes but that seems possible.
You may use the following.
For the search box I use its id to target the element. This is normally unique on a document and the fastest selector method.
For the search button I use a CSS attribute + value selector of
[src*='/image/search.gif']
This targets the src attribute [] of the element by its value. * means contains. The selector looks for a src attribute containing /image/search.gif in its value.
You can observe the attribute here:
Option Explicit
Sub Searchstockcode()
Dim SearchString As String, SearchBox As Object, SearchButton As Object, ie As Object
SearchString = "700"
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
ie.navigate "http://www.hkexnews.hk/listedco/listconews/advancedsearch/search_active_main.aspx"
While ie.Busy Or ie.readyState < 4: DoEvents: Wend
Set SearchBox = ie.document.getElementById("ctl00_txt_stock_code")
SearchBox.Value = SearchString
Set SearchButton = ie.document.querySelector("[src*='/image/search.gif']")
SearchButton.Click
While ie.Busy Or ie.readyState < 4: DoEvents: Wend
Stop '<==Delete me
'other code
ie.Quit
End Sub

Excel VBA object required error with IE automation

So I have some code to go to a website and then login. Then I need it to go to the password change URL. Sometimes when I run the code, I get the following error:
Runtime Error 424: Object Required
Sometimes the debugger says it is the first getelementbyid statement, but most times it says the issue is the last three getelementbyid statements. The code is below:
Dim ie As Variant
Dim strURL As String
Sub login()
Set ie = New SHDocVw.InternetExplorer
ie.Visible = True
ie.navigate "https://minecraft.net/profile"
While ie.Busy
DoEvents
Wend
ie.document.getElementById("username").Value = "ddd"
ie.document.getElementById("password").Value = "ddddddddddd"
Dim htmlForm As HTMLFormElement
Set htmlForm = ie.document.getElementById("loginForm")
htmlForm.submit
' **********************************************************************
'IE.Document.getElementById("username").Value = "ddddd"
' IE.Document.getElementById("password").Value = "ddddd"
' IE.Document.getElementById("signin").Click
'**********************************************************************
'Pause while page loads
Application.Wait (Now + #12:00:03 AM#)
ie.navigate "https://minecraft.net/profile/password"
ie.document.getElementById("oldPassword").Value = "oldpass"
ie.document.getElementById("password").Value = "enwapss"
ie.document.getElementById("passwordConfirmation").Value = "enwapss"
Set htmlForm = ie.document.getElementById("loginForm")
htmlForm.submit
End Sub
Thanks in advance!
It may be that the website isn't in a ready state, as in the site hasn't fully loaded when it's attempting to input the values.
After
ie.navigate "https://minecraft.net/profile/password"
Try adding
Do Until Not ie.Busy And ie.readyState = 4
DoEvents
Loop
This will loop until the webpage has loaded similar, to the way you've done it in your above code with
Application.Wait (Now + #12:00:03 AM#)

Resources