Filling Internet Explorer inputbox - vba

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

Related

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.

Using VBA to click on a link/button in an IE web page

I have read a lot of information on using VBA to click on a link in IE, but I cannot get it to work in my case.
The relevant HTML code is as follows:
<div id="LinkButton_3" widgetId="LinkButton_3">
<a class="Row_Icon" data-dojo-attach-point="linkNode" data-dojo-attach-event="onClick:_onLinkButtonClicked">Company XYZ. - #12345</a>
</div>
The VBA code I have tried, with 3 different attempts noted, is as follows:
Dim ieApp As SHDocVw.InternetExplorer
Dim ieDoc As MSHTML.HTMLDocument
Dim button As HTMLInputButtonElement
Dim div As HTMLDivElement
' Create a new instance of IE
Set ieApp = New InternetExplorer
' Uncomment this line for debugging purposes.
ieApp.Visible = True
' Go to the page we're interested in.
ieApp.Navigate "MyURL.com"
Do While ieApp.Busy: DoEvents: Loop
Do Until ieApp.ReadyState = READYSTATE_COMPLETE: DoEvents: Loop
Set ieDoc = ieApp.Document
' Try #1.
' Nothing happened on the web page after these 3 lines were executed.
Set button = ieDoc.getElementById("LinkButton_3")
button.Focus
button.Click
' Try #2
' Nothing happens - button is not clicked.
Set div = ieDoc.getElementById("LinkButton_3")
div.FireEvent "onClick"
' Try #3
' Nothing happens - button is not clicked.
div.Click
' Close the instance of IE.
ieApp.Quit
' Clean up.
Set ieApp = Nothing
Set ieDoc = Nothing
Any thoughts on what I might be doing wrong or other suggestions would greatly be appreciated.
TMc
You can also use a CSS querySelector of #LinkButton_3 a. This is the a tag within the element with id LinkButton_3. The "#" means id. The " a" means a tag within.
.querySelector method belong to the HTMLDocument.
You can do:
ieDoc.querySelector("#LinkButton_3 a").Click
<div id="LinkButton_3" widgetId="LinkButton_3">
<a class="Row_Icon" data-dojo-attach-point="linkNode" data-dojo-attach-event="onClick:_onLinkButtonClicked">Company XYZ. - #12345</a>
</div>
What you want to click on is the anchor (a) tag, not the div. So, you can find the div with its id and then click on its one and only child with
button.Children(0).Click

VBA Internet Explorer clicking on Text Error

I am new to VBA and it would be great if you can help me on resolving this issue.
I am trying to click on a Text on an IE SharePoint webpage. I am am able to navigate to IE browser, but I am getting a VBA error for clicking the text "Americas" highlighted in Yellow in attached Screenshot. I need help with the IE.Document part of the code at the end of VBA code below. I assume GetElementbyID and GetElementByTagName are correct from HTML code below.
Error - Method Document of Object "IEwebBrowser"Failed
VBA Code:
Private Sub UploadFile()
Dim i As Long
Dim IE As Object
Dim Doc As Object
Dim objElement As Object
Dim objCollection As Object
Dim buttonCollection As Object
Dim AllSpanElements
Dim Span
' Create InternetExplorer Object
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
' Send the form data To URL As POST binary request
IE.navigate "URL"
' Wait while IE loading...
While IE.Busy
DoEvents
Wend
' I AM GETTING ERROR HERE
Set AllSpanElements = IE.Document.getElementById("ext-gen1271").getElementsByTagName("div")
AllSpanElements.Click
Set IE = Nothing
Set objElement = Nothing
End Sub
HTML CODE
<table class="x-grid-table x-grid-table-resizer" border="0" cellspacing="0" cellpadding="0" style="width:10000px;"><tbody>
<tr class="x-grid-row x-grid-row-selected x-grid-row-focused" id="ext-gen1271">
<td class="x-grid-cell-treecolumn x-grid-cell x-grid-cell-treecolumn-1030 x-grid-cell-first">
<div class="x-grid-cell-inner " style="text-align: left; ;"><img src="data:image/gif;base64,R0lGODlhAQABAID/AMDA" class="x-tree-elbow-plus x-tree-expander">
<img src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAA" class="x-tree-icon x-tree-icon-parent ">
Americas</div>
</td>
</tr>
</tbody>
</table>
Give this a shot, I cleaned up the code slightly and I'm trying a slightly different approach. Basically I'm iterating over each element on the page, then clicking it when the InnerText has "Americas" contained with in it.
It may not be the InnerText Property you want to check, it might be the Value or Title so you will need to check that.
Here's the code:
Private Sub UploadFile()
Dim IE As Object: Set IE = CreateObject("InternetExplorer.Application")
Dim Elements As Object
Dim Element As Object
With IE
.Visible = True
' Send the form data To URL As POST binary request
.navigate "URL"
' Wait while IE loading...
While .Busy Or .readystate <> 4
Application.Wait (Now() + TimeValue("00:00:01"))
DoEvents
Wend
End With
' I AM GETTING ERROR HERE
Set Elements = IE.Document.getElementsByTagName("*") ' * is all elements
For Each Element In Elements
If InStr(1, Element.innerText, "Americas") > 0 Then ' If the element has the word Americas...click it
Element.Focus
Element.Click
Element.FireEvent ("OnClick")
End If
Next
'Clean up
Set IE = Nothing
End Sub

Data entry using 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.

Visual Basic Word Get Elements from a Website

I'm trying to import text from specific div´s of a website to a bookmark in a Word document and I'm stuck with reading the HTML from a website. I tried 100 tutorials all for VBA Excel (maybe that's why) and always the same result.
Let's say that I have a site like:
<html>
<div id = "test">
this is an example text
</div>
</html>
and here is my VBA Code:
Sub read_html()
Set objIE = CreateObject("InternetExplorer.Application")
Dim htmlOut As String
With objIE
.Navigate "http://blabla.net/testy/test.html"
Do
Loop Until Not .Busy
htmlOut = .Document.getElementsByName("test")
.Quit
MsgBox "example:" & htmlOut
End With
Set iexpl = Nothing
End Sub
The MsgBox returns: example [object]
getElementsByName will return a collection of all the elements with that name. Even if there's only one, it still returns a collection. Just a collection with one item in it. You can get the first element in the collection like
htmlOut = .Document.getElementsByName("test").Item(0).InnerText
The .Item(0) will return the first item and the .InnerText will return a string.