Data entry using VBA - vba

I'm trying to submit data on some HTML page using VBA. The HTML page contains some Java scripts and it is a secured page.
I tried to use the codes below, but it only opens the website window and doesn't fill out any texts on the page.
I know I still need to add codes for loginname/password valification, msgbox (for stop control), submit button click codes.
Could someone help?
Sub Tes()
Dim IE As Object
Dim TrackID As Object
Set IE = CreateObject("InternetExplorer.Application")
IE.navigate "https://abcxxx.com/xxxx/xxxxxxx/Pages/CustInfo.aspx"
IE.Visible = True
Do Until IE.readystate = 4: DoEvents: Loop
Set TrackID = IE.document.getelementbyid("abc00_abc001_234_5678_81b0_txtCustomerNo")
TrackID.Value = Range("A2").Value
TrackID.form.submit
End Sub
:::header informaion:::
<head id="ctl00_HEAD1"></head>
<body onload="javascript:if (typeof(_spBodyOnLoadWrapper) != 'undefined') _spBodyOnLoadWrapper();" scroll="yes">
<form id="xxxForm" onsubmit="javascript:return WebForm_OnSubmit();" action="/xxxx/xxxxxxx/Pages/CustInfo.aspx" method="post" name="xxxForm">
<div></div>
<script type="text/javascript"></script>
<script type="text/javascript" src="/WebResource.axd?d=l9_fanifTkSGPMjqQJxGzkhm0A9CUTq0e2fMvidi8…mlhiyeYfbecR_SY_yD1HlOp8dnx1WI0dTdidvw1&t=634605258709717464"></script>
<script></script>
<script language="JavaScript" type="text/JavaScript"></script>
<script></script>
<script type="text/javascript" src="/WebResource.axd?d=wAwuGCcAd8-EEHMrxtDWcHPpbrIz4dtxvSld4vGrJ…1WyuhxU_XTb_KI5FuNTSBKr7UJL75sqY7cXp281&t=634605258709717464"></script>
<script type="text/javascript" src="/WebResource.axd?d=zXhglhR5yBgSmErqplHM82fD2Jq9gJIaO6HzhlL5E…MnKMvLQ-2WyEUUcPnCWpgOMpxqYDqDmsApddeA1&t=634605258709717464"></script>
<script type="text/javascript"></script>

You have redacted the actual website address so no one can actually find out what you are doing wrong. But your method appears to be fine - the same code works for me on another website:
Sub main()
Dim IE As Object
Dim TrackID As Object
Set IE = CreateObject("InternetExplorer.Application")
IE.navigate "http://www.wikipedia.org/"
IE.Visible = True
Do Until IE.readystate = 4: DoEvents: Loop
Set searchInput = IE.document.getelementbyid("searchInput")
searchInput.Value = "Visual Basic for Applications"
searchInput.form.submit
End Sub

You have a form with the redacted name xxxForm. If all of the input elements you wish to set values to are children of this form, I would set an object to the form and use it as the parent.
I prefer to work with the Microsoft HTML element declarations. You may need the VBE's Tools ► References Microsoft Internet Controls and Microsoft HTML object library added to your project.
Sub Tes()
Dim IE As new SHDocVw.InternetExplorer
Dim eTrackForm As MSHTML.IHTMLElement
Set IE = CreateObject("InternetExplorer.Application")
IE.navigate "https://abcxxx.com/xxxx/xxxxxxx/Pages/CustInfo.aspx"
IE.Visible = True
IE.Silent = True
Do while ie.busy or IE.readystate <> 4: DoEvents: Loop
Set eTrackForm = IE.document.getelementbyid("xxxForm")
if not eTrackForm is nothing then
with eTrackForm
.getelementbyid("ID_of_the_name_input").value = "Me"
.getelementbyid("ID_of_the_password_input").value = "MyPass"
.getelementbyid("abc00_abc001_234_5678_81b0_txtCustomerNo").value = Sheets("Sheet1").Range("A2").Value
.submit
end with
end if
End Sub
That looks like it should get you past the first credentials input form once you've changed my generic credentials and input element IDs to the actuals ones. There is nothing magical here. In fact there is very little difference between this, your code or other code submitted here as a possible solution.

Related

VBA Web Scraping: Clicking on Download Bar (Save file) when ie.visible=False

Is there any way to click on Save button when IE.visible=False?
I tried this option that only worked when ie.visible =True
Excel VBA to Save As from IE 11 Download Bar
Generic example:
Sub scrape_ex()
Dim oHTML_Element As IHTMLElement
Dim oHTML_Element_signo As IHTMLElement
Dim oBrowser As InternetExplorer
Dim ie As Variant
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = False
ie.navigate "URL"
While ie.readyState <> READYSTATE_COMPLETE And ie.readyState <> READYSTATE_LOADED
DoEvents
Wend
For Each oHTML_Element In ie.document.getElementsByClassName("class")
If oHTML_Element.Title = "my_download" Then
oHTML_Element.Click
Application.Wait (Now + TimeValue("00:00:03"))' Download Bar starts
Exit For
End If
Next
''clicking on Save that work with ie.visible=False ???
Edit:
Clicking the button in the page works perfect, it is not a problem in the web page. I want to click on "Save" when IE download bar starts (with IE.visible=False, so I cant use SendKeys):
If you just use automation to click the button in the page, then it should work, even in the case of ie.Visible = False.
This is my simple test and it works fine:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>test</title>
</head>
<body>
<button type="button" id="ieDown" onclick="onSaveInIE()">In IE</button>
<script>
function onSaveInIE() {
var csvWindow = openNewWindowObj();
csvWindow.document.execCommand('SaveAs', null, 'abc.html');
csvWindow.close();
}
function openNewWindowObj() {
var newWindowObj = window.open("window.html", "New popup Window", 'height=200,width=150')
newWindowObj.focus();
return newWindowObj;
}
</script>
</body>
</html>
VBA code:
Sub scrape_ex()
Dim oHTML_Element As IHTMLElement
Dim oHTML_Element_signo As IHTMLElement
Dim oBrowser As InternetExplorer
Dim ie As Variant
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = False
ie.navigate "https://localhost:44315/Index.html"
While ie.readyState <> READYSTATE_COMPLETE And ie.readyState <> READYSTATE_LOADED
DoEvents
Wend
For Each oHTML_Element In ie.document.getElementsByTagName("button")
If oHTML_Element.ID = "ieDown" Then
oHTML_Element.Click
Application.Wait (Now + TimeValue("00:00:03")) ' Download Bar starts
Exit For
End If
Next
End Sub
Note: I am not sure how your page is designed, so I modified some VBA code to get page elements in this test.
Edit:
If you need to use automation to click a button in the `IE Application UIwhen it is not visible, it is impossible. Because the keys will go to the window that has focus. If the window is invisible it cannot have the focus, therefore cannot receive the keys.

VBA Object doesn't support property or method when filling webform

I'm trying to use the Excel VBA to fill a web form and then scrape some data.
I am stuck on how to put in the value that I want to search for.
In the source code, there is no id or name that I can use.
I tried with a class name but it is not working.
" Source code:
<input type="text" style="height: 28px; width: 170px; padding-left: 5px;" ng-model="model.FieldValue" placeholder="Enter a Value" ng-show="model.searchOptions.length && model.searchOptions != 'SQE Quote Id'" class="ng-pristine ng-valid ng-touched">
Sub Macro1()
Dim i As Long
Dim URL As String
Dim IE As Object
Dim objElement As Object
Dim objCollection As Object
Dim html As HTMLDocument
Set IE = New InternetExplorerMedium
'Set IE.Visible = True to make IE visible, or False for IE to run in the background
IE.Visible = True
'Define URL
URL = "xxxx"
'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
'
Set html = IE.Document
IE.Document.getElementById("searchOptions").Click (2)
IE.Document.getElementById("searchOptions").Value = "Access Qref"
IE.Document.getelemenetbyclassname("ng-pristine ng-valid ng-touched").Value = "00000"
'
End Sub
Try to put '.' between class names like below is working on my side.
IE.Document.querySelector("input.ng-pristine.ng-valid.ng-touched").Value = "00000"
Modified example:
Sub Macro1()
Dim i As Long
Dim URL As String
Dim IE As Object
Dim objElement As Object
Dim objCollection As Object
Dim html As HTMLDocument
Set IE = New InternetExplorerMedium
'Set IE.Visible = True to make IE visible, or False for IE to run in the background
IE.Visible = True
'Define URL
URL = "D:\Backup20190913\tests\353.html"
'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
Set html = IE.Document
IE.Document.querySelector("input.ng-pristine.ng-valid.ng-touched").Value = "00000"
End Sub
Output:
The error is as it says. The method you are trying does not exist for that node. You are fine using a multi-valued class (more than one class name for the element; denoted by several strings separated by spaces within the class attribute of the element) inside of the correct method getElementsByClassName, rather than getelemenetbyclassname then you need to index into the returned collection in order to exercise the .click method.
IE.Document.getElementsByClassName("ng-pristine ng-valid ng-touched")(0).Value = "00000" '0 for first node
However, as in other answer, if using querySelector/querySelectorAll to match by css class selector then a multi-valued class must have the spaces between replaced with ".". Better still, try to select as few of those class names inside the multi-value as possible and the most selective which still match on the required node(s)
And please, I write this a lot on SO, use a proper page load wait:
While IE.busy Or IE.readystate <> 4: DoEvents: Wend

make a program intract with something shown in web page

I want to build a program that interact with a web page, For example :
I have 3 lines A, B and C shown in the browser, when line A turned to green
I want to some value in variable. and so on with the other lines.
I don't want to open the page in my code, the page is already opened in windows explorer, and i want my code to interact with it where it is.
I hope that I explained my question clearly.
I try to access already opened web page using VBA code below but it is not able to get the text.
Sub demo()
Dim IEWindows As SHDocVw.ShellWindows
Dim IEwindow As SHDocVw.InternetExplorer
Dim IEDocument As MSHTML.HTMLDocument
Dim BreadcrumbDiv As MSHTML.HTMLElementCollection
Set IEWindows = New SHDocVw.ShellWindows
For Each IEwindow In IEWindows
'Debug.Print (IEwindow.LocationURL)
If InStr(IEwindow.LocationURL, "file:///C:/Users/Administrator/Desktop/demo17.html") <> 0 Then ' Found it
Set IEDocument = IEwindow.Document
Set BreadcrumbDiv = IEDocument.getElementById("demo1")
Debug.Print (IEwindow.Document.getElementById("data2").Value)
End If
Next
End Sub
As a work around, You can try to refer example below.
Sub demo()
Dim myIE As Object
Dim myIEDoc As Object
'Start Internet Explorer
Set myIE = CreateObject("InternetExplorer.Application")
'if you want to see the window set this to True
myIE.Visible = True
'Now we open the page we'd like to use as a source for information
myIE.navigate "C:\Users\Administrator\Desktop\demo17.html"
'We wait for the Explorer to actually open the page and finish loading
While myIE.Busy
DoEvents
Wend
'Now lets read the HTML content of the page
Set myIEDoc = myIE.Document
'Then we'll get something from teh inner page content by using the ID
If myIEDoc.all.Item("data1").Checked = True Then
Debug.Print (myIEDoc.getElementById("data1").Value)
ElseIf myIEDoc.all.Item("data2").Checked = True Then
Debug.Print (myIEDoc.getElementById("data2").Value)
Else
Debug.Print (myIEDoc.getElementById("data3").Value)
End If
End Sub
<!doctype html>
<head>
</head>
<body>
<input type="checkbox" id="data1" name="data" value="This is line 1."> This is line 1.<br>
<input type="checkbox" id="data2" name="data" value="This is line 2." checked> This is line 2.<br>
<input type="checkbox" id="data3" name="data" value="This is line 3."> This is line 3.<br>
</body>
</html>
Output in immediate window.
Further, you can try to modify code as per your requirement.

Clicking link within a HTML List

I'm trying use Excel's IE automation tools to click a website link within a users profile of this site. Using the following in VBA:
ie.Document.getElementsByClassName("website")(0).getElementsByTagName("a")(0).Click
But keep getting the Runtime error '438' when ran. Any advice on how to fix this or if clicking this link is even possible please? Thanks.
EDIT (Full Code):
Dim ie As InternetExplorer
Dim html As HTMLDocument
Set ie = New InternetExplorer
ie.Visible = True
ie.Navigate "site"
Do While ie.READYSTATE <> READYSTATE_COMPLETE
DoEvents
Loop
Set objA = ie.Document.getElementsByClassName("website")(0) 'GETTIN ERROR HERE
objA.getElementsByTagName("a")(0).Click
End Sub
It would appear that there were a few minor issues here.
One being that .Click doesn't always function properly, I would suggest using .getAttribute("href") combined with ie.Navigate()
Another being that you seem to define html then never use it again.
The following code works:
Sub test()
Dim ie As InternetExplorer
Dim html As HTMLDocument
Set ie = New InternetExplorer
ie.Visible = True
ie.Navigate "http://beverlyhills.yourkwoffice.com/mcj/user/AssociateSearchSubmitAction.do?orgId=5058&lastName=&firstName=&rows=100"
Do While ie.READYSTATE <> READYSTATE_COMPLETE
DoEvents
Loop
Set html = ie.Document
Set objA = html.getElementsByClassName("website")(0).getElementsByTagName("a")(0)
ie.Navigate (objA.getAttribute("href"))
End Sub

Filling Internet Explorer inputbox

I read so many answers to my problem but somehow if I try to "mimic" what I see, I still am not able to do what I need.
The problem is very simple: fill an inputbox on an opened IE page.
Result: the code gets stuck on the line with getelementbyid showing runtime error 424 (object required).
Private Sub AddInfoFromIntranet()
Dim ie As Object
Set ie = CreateObject("internetexplorer.application")
Application.SendKeys "{ESC}" ' I need this to ignore a prompt
With ie
.Visible = True
.navigate "{here goes the address of my website}"
Do Until Not .Busy And .readyState = 4
DoEvents
Loop
.document.getelementbyid("Nachnamevalue").Value = "{here goes whar I want to insert}"
End With
Set ie = Nothing
End Sub
Internet Explorer libraries were naturally imported (otherwise the "internetexplorer.application" wouldn't work.
I am positive that the field I want to fill is called "Nachnamevalue" as from what I learned this morning taking a look around the internet.
The html code of my webpage (only the interesting piece) looks like this:
<!DOCTYPE html>
<html>
<head>
<title></title>
<style>
'{here there are info on the style, which i'm gonna ignore}
</style>
</head>
<body bgcolor="#ffffcc"><table width="1000"><tbody><tr><td>
<form name="Suchform" action="index.cfm" method="get" target="bottom_window">
Nachname:
<select name="Nachnamepulldown" class="font09px" onchange="wait_and_search()">
<option value="BEGINS_WITH">beginnt mit
<option value="EQUAL">ist
<option value="CONTAINS">enthält
</option></select>
<input name="Nachnamevalue" onkeyup="wait_and_search()" type="text" size="8">
Abteilung:
<select name="Abteilungpulldown" class="font09px" onchange="wait_and_search()">
<option value="BEGINS_WITH">beginnt mit
<option value="EQUAL">ist
<option value="CONTAINS">enthält
</option></select>
<input name="Abteilungvalue" onkeyup="wait_and_search()" type="text" size="3">
<input name="fuseaction" type="hidden" value="StdSearchResult">
<input type="submit" value="suchen">
<script language="JavaScript" type="text/JavaScript">
document.Suchform.Nachnamevalue.focus();
</script>
</form>
</td></tr></tbody></table></body>
</html>
There is also (I don't know if it can help) an "embedded" javascript that brings results of a search up every time at least 2 characters in the "Nachnamevalue" inputbox are written.
What am I doing wrong?
EDIT:
When I try to execute the Sub step-by-step, I get the following:
Set Doc = ie.document
? Doc
[object HTMLDocument]
( in the watchlist it is an object without any variables inside )
GetElementById gets an element by its id attribute, but "Nachnamevalue" is the value of the name attribute.
To use the name:
.document.Forms("Suchform").Elements("Nachnamevalue").value = "xxx"
This worked for me. The code uses HTML from your question in file c:\Temp\page1.html.
Option Explicit
' Add reference to Microsoft Internet Controls
' Add reference to Microsoft HTML Object Library
Sub AddInfoFromIntranet()
Dim ie As SHDocVw.InternetExplorer
Dim doc As MSHTML.HTMLDocument
Dim elements As MSHTML.IHTMLElementCollection
Dim nachnameValueInput As MSHTML.HTMLInputElement
Set ie = New SHDocVw.InternetExplorer
With ie
.Visible = True
.navigate "c:\Temp\page1.html"
Do Until Not .Busy And .readyState = 4
DoEvents
Loop
Set doc = .document
Set elements = doc.getElementsByName("Nachnamevalue")
If Not elements Is Nothing Then
Set nachnameValueInput = elements(0)
If Not nachnameValueInput Is Nothing Then _
nachnameValueInput.Value = "{here goes whar I want to insert}"
End If
.Quit
End With
Set ie = Nothing
End Sub
To check the names of all input elements which exist at the momonet you execute the VBA code on the page you could use getElementsByTagName("input").
Set elements = doc.getElementsByTagName("input")
If Not elements Is Nothing Then
Dim inputElement
For Each inputElement In elements
Debug.Print inputElement.Name
Next inputElement
End If
You can try cycling all input and selecting the one is named as you need:
Set Elements = IE.document.getelementsbytagname("Input")
For Each Element In Elements
If Element.Name = "Nachnamevalue" Then
Element.Value = {Here your value}
Exit For
End If
Next Element