.Click action not doing anything - IE Simulation - vba

I'm trying to simulate the interaction with Google through the IE app and going through the DOM to get the classes I need and all is fine, stepping though the code, except the .Click action which doesn't cause a crash but it doesn't do anything (page doesn't navigate) - Code and screenshot of HTML below:
Option Explicit
Private Sub Test_Automation()
Dim ie, doc, eInput, eButton, eButtons As Object
Dim sURL, sTest As String
Set ie = CreateObject("internetexplorer.application")
sURL = "https://www.google.co.uk/?gfe_rd=cr&ei=IpDvWK72LsjCaJCbjKAL&gws_rd=ssl"
sTest = "Test"
With ie
.Visible = True
.Navigate sURL
End With
Do While ie.Busy Or ie.readyState <> 4
DoEvents
Loop
Set doc = ie.document
Set eInput = doc.getElementByid("lst-ib")
Set eButtons = doc.getElementsByTagName("input")
eInput.Value = sTest
For Each eButton In eButtons
If (eButton.getattribute("name") = "btnK") Then
eButton.Click
Exit For
End If
Next
End Sub
Any advise on what I'm doing wrong would be great!

You can get rid of your For...Next loop at the bottom and replace it with this to click the button:
doc.forms(0).submit
The 0 can be changed to another number (such as 1 or 2) to click on a different button. If there are multiple buttons on a page that can be clicked on it will just take some trial and error to find out which number matches the button you want to click.

Related

How to pause Excel VBA and wait for user interaction before loop

I have a VERY basic script I am using with an excel spreadsheet to populate a form. It inserts data into the fields then waits for me to click the submit button on each page. Then it waits for the page to load and fills in the next set of fields. I click submit, next page loads, etc. Finally, on the last two pages I have to do some manual input that I can't do with the spreadsheet. Code thus far is shown below.
My question is, at the end of what I have there now, how can I make the system wait for me to fill out that last page, then once I submit it realize that it has been submitted and loop back to the beginning, incrementing the row on the spreadsheet so that we can start over and do the whole thing again for the next student?
As you will probably be able to tell I am not a programmer, just a music teacher who does not savor the idea of filling out these forms manually for all 200 of my students and got the majority of the code you see from a tutorial.
Function FillInternetForm()
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
'create new instance of IE. use reference to return current open IE if
'you want to use open IE window. Easiest way I know of is via title bar.
IE.Navigate "https://account.makemusic.com/Account/Create/?ReturnUrl=/OpenId/VerifyGradebookRequest"
'go to web page listed inside quotes
IE.Visible = True
While IE.busy
DoEvents 'wait until IE is done loading page.
Wend
IE.Document.All("BirthMonth").Value = "1"
IE.Document.All("BirthYear").Value = "2000"
IE.Document.All("Email").Value = ThisWorkbook.Sheets("queryRNstudents").Range("f2")
IE.Document.All("Password").Value = ThisWorkbook.Sheets("queryRNstudents").Range("e2")
IE.Document.All("PasswordConfirm").Value = ThisWorkbook.Sheets("queryRNstudents").Range("e2")
IE.Document.All("Country").Value = "USA"
IE.Document.All("responseButtonsDiv").Click
newHour = Hour(Now())
newMinute = Minute(Now())
newSecond = Second(Now()) + 3
waitTime = TimeSerial(newHour, newMinute, newSecond)
Application.Wait waitTime
IE.Document.All("FirstName").Value = ThisWorkbook.Sheets("queryRNstudents").Range("a2")
IE.Document.All("LastName").Value = ThisWorkbook.Sheets("queryRNstudents").Range("b2")
IE.Document.All("Address1").Value = "123 Nowhere St"
IE.Document.All("City").Value = "Des Moines"
IE.Document.All("StateProvince").Value = "IA"
IE.Document.All("ZipPostalCode").Value = "50318"
End Function
I would use the events of the IE, more specifically the form, something like this, using MSHTML Controls library.
Private WithEvents IEForm As MSHTML.HTMLFormElement
Public Sub InternetExplorerTest()
Dim ie As SHDocVw.InternetExplorer
Dim doc As MSHTML.HTMLDocument
Set ie = New SHDocVw.InternetExplorer
ie.Visible = 1
ie.navigate "http://stackoverflow.com/questions/tagged/vba"
While ie.readyState <> READYSTATE_COMPLETE Or ie.Busy
DoEvents
Wend
Set doc = ie.document
Set IEForm = doc.forms(0)
End Sub
Private Function IEForm_onsubmit() As Boolean
MsgBox "Form Submitted"
End Function

Need to use VBA to select an option from a drop down list in Internet Explorer

I have successfully got my code to open IE, navigate to the webpage I need, and login. I now need to select an option from a drop down list - please see the following html code:
html code for the list
How do I select the "TPS Managed Conservative - Dec 11" option from the dropdown.
My code so far:
Sub Strategic_Alpha_Monthly_Pivots_1_MASTER()
' open IE, navigate to the desired page and loop until fully loaded
Dim ie As Object
Set ie = CreateObject("InternetExplorer.Application")
my_url = "http://analytics.financialexpress.net/login.aspx"
With ie
.Visible = True
.navigate my_url
Do Until Not ie.Busy And ie.readyState = 4
DoEvents
Loop
End With
' Input the userid and password
ie.document.getElementById("txtPassword").Value = "xxxxx"
' Click the "Search" button
ie.document.getElementById("btnAction").Click
Do Until Not ie.Busy And ie.readyState = 4
DoEvents
Loop
ie.document.getElementById("ListPortfolio").Select
End Sub
selectedIndex or Value could be used. According to the screenshot the value 983678630 can be used. HTH
If Not VBA.IsNull(ie.document.getElementById("ListPortfolio")) Then
Dim htmlSelect
Set htmlSelect = ie.document.getElementById("ListPortfolio")
' htmlSelect.selectedIndex = 6
htmlSelect.Value = 983678630
Else
MsgBox "Element 'ListPortfolio' was not found", vbExclamation
End If

Error received when downloading a file from web when only submit button is present

I need to download a file from web, but only a submit button is present.
There is no url for reaching the file directly.
I tried to catch the button by its id, but VBA drops run-time error '424' (Object required).
Here is the code:
Sub keler_submit()
Application.ScreenUpdating = False
Set IE = CreateObject("InternetExplorer.Application")
IE.navigate "https://www.keler.hu/T%C3%A1rsas%C3%A1gi%20esem%C3%A9nyek/"
While IE.Busy
DoEvents
Wend
Set doc = IE.document
gomb = doc.getElementById("ctl00_wpm_UserControlPortlet1682286671_ctl00_wpm_UserControlPortlet1682286671_userControlPortlet_DownloadButton1")
gomb.submit
Set IE = Nothing
Application.ScreenUpdating = True
End Sub
Thank you in advance
These lines are incorrect:
gomb = doc.getElementById("ctl00_wpm_UserControlPortlet1682286671_ctl00_wpm_UserControlPortlet1682286671_userControlPortlet_DownloadButton1")
gomb.submit
doc.getElementById returns an object type so you need to use Set to assign it to a variable. The element you are trying to access should then be clicked instead of submitted. There's a high probability that IE will display a "Do you wish to open or save this file" dialog so start by making the IE object visible to show this:
IE.visible = True
Set gomb = doc.getElementById("ctl00_wpm_UserControlPortlet1682286671_ctl00_wpm_UserControlPortlet1682286671_userControlPortlet_DownloadButton1")
gomb.click
You need a loop in there as well to ensure page has loaded. The following works:
Option Explicit
Public Sub GetInfo()
Dim IE As New InternetExplorer
With IE
.Visible = True
.navigate "https://www.keler.hu/T%C3%A1rsas%C3%A1gi%20esem%C3%A9nyek/"
While .Busy Or .readyState < 4: DoEvents: Wend
Do
Dim b As Object
On Error Resume Next
Set b = .document.getElementById("ctl00_wpm_UserControlPortlet1682286671_ctl00_wpm_UserControlPortlet1682286671_userControlPortlet_DownloadButton1")
On Error GoTo 0
Loop While b Is Nothing
b.Click
'Handle save button
'Quit '<== Remember to quit application
End With
End Sub

Using VBA to control IE to login to a website

I'm new to this, but I'm practising, and it seemed to me that logging into my Institute's website ought to be the easiest thing in the world. Right? Well, it's driving me crazy.
Specifically, the program fails at
page.Document.getElementById("edit-name").Value = 1234
Run time error 424 "object required"
Also, and I'm not sure if this is related, when I type "page." the VBA editor helpfully provides a list of options, and I select Document. When I type the second "." to get "page.Document." I do not then get a further list of options.
Here's the complete program, and thanks for any help
Public Sub MyLogin()
'This uses early binding
'In menu at top of page - Tools...Reference.... Microsoft Internet Controls and Microsoft HTML Object Library must both be ticked
Dim page As InternetExplorer
Set page = New InternetExplorer
page.Visible = True
Dim MyLink As Object
'Login at Institute website
page.Navigate "http://www.actuaries.org.uk/"
Do While page.Busy Or page.ReadyState <> 4
DoEvents
Loop
For Each MyLink In page.Document.Links
If InStr(MyLink.href, "/user") Then MyLink.Click: Exit For
Next MyLink
Do While page.Busy Or page.ReadyState <> 4
DoEvents
Loop
'Are now at the login page
'Enter username, password and click
page.Document.getElementById("edit-name").Value = 1234
page.Document.getElementById("edit-pass").Value = "01/01/1977"
page.Document.getElementById("edit-submit").Click
End Sub
Specifically, the program fails at page.Document.getElementById("edit-name").Value = 1234
Nothing wrong with that line.
One reason that I can think of is that your For Loop is not executed. To avoid that, directly navigate to the relevant url. This trimmed version works for me.
Sub Sample()
Dim sUrl As String: sUrl = "https://www.actuaries.org.uk/user"
Dim ie As Object
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
ie.navigate sUrl
Do While ie.readystate <> 4: DoEvents: Loop
ie.Document.getElementById("edit-name").Value = 1234
End Sub

VBA - Addressing Internet Explorer tabs

Strangely enough I didn't find any information on the topic and I'm currently stuck at the point where I managed to open a new tab in an instance of IE by programmatically clicking a button, but I haven't the faintest clue of how to address the new tab in order to get information from there (the button basically brings up a new tab with the result of a search).
This is basically a straightforward question, but I'm including my code anyway:
Sub AddInfoFromIntranet()
Dim Ie As SHDocVw.InternetExplorer
Dim URL As String
Dim iFrames As MSHTML.IHTMLElementCollection
Dim iFrame As MSHTML.HTMLFrameElement
Dim Doc As MSHTML.HTMLDocument
Dim InputBox As MSHTML.IHTMLElementCollection, htmlButton, allTags, Tag
' Opens Intranet - yeah, sadly it's not a public web page
URL = "{My intranet website}"
Set Ie = New SHDocVw.InternetExplorer
With Ie
.navigate URL
.Visible = True
While .Busy Or .readyState <> READYSTATE_COMPLETE: DoEvents: Wend
Set Doc = .document
End With
' Gets top_window frame and navigates to it, then inserts the name to search
Set iFrames = Doc.getElementsByName("top_window")
If Not iFrames Is Nothing Then
Set iFrame = iFrames(0)
Ie.navigate URL & iFrame.src
While Ie.Busy Or Ie.readyState <> READYSTATE_COMPLETE: DoEvents: Wend
Set InputBox = Doc.getElementsByName("Nachnamevalue")
If Not InputBox Is Nothing Then InputBox(0).Value = "test"
' Clicks on "search"
Set allTags = Doc.getElementsByTagName("input")
For Each Tag In allTags
If Tag.Value = "suchen" Then
Tag.Click
Exit For
End If
Next
' Here a new tab is opened, must find info in this tab
While Ie.Busy Or Ie.readyState <> READYSTATE_COMPLETE: DoEvents: Wend
' HERE I HAVE NO CLUE WHAT TO WRITE. THE CODE ABOVE WORKS FLAWLESSLY
End If
Set Doc = Nothing
Set iFrames = Nothing
Set iFrame = Nothing
Set InputBox = Nothing
Set allTags = Nothing
Set Ie = Nothing
Ie.Quit
End Sub
Now, is there a way to address a tab by: 1) its name (and where do I find it) 2) its position in browser 3) the status (if it is "active") ?
Bonus questions: since I am new to VBA and Internet Explorer interaction, what exactly are the variables: htmlButton, allTags, Tag ? Also, could anyone explain if I need to set all the variables at the end to nothing, or I just need to set the Internet Explorer to nothing?
Thanks in advance!
See below for a function you can use to get an open IE document window - I don't think IE exposes any simple (VBA-accessible) API for working directly with tabs or determining whether a specific tab is active.
allTags is a collection of DOM elements with type "" , and Tag is a single memeber of that collection.
You do not have to set objects to Nothing before exiting a Sub (though some people still do that) - the VBA runtime will take care of that for you.
Sub TestGetIE()
Dim IE As Object
Set IE = GetIE("http://stackoverflow.com")
If Not IE Is Nothing Then
IE.document.execCommand "Print", False, 0
End If
End Sub
'Get a reference to an open IE window based on its URL
Function GetIE(sLocation As String) As Object
Dim objShell As Object, objShellWindows As Object, o As Object
Dim sURL As String
Dim retVal As Object
Set retVal = Nothing
Set objShell = CreateObject("Shell.Application")
Set objShellWindows = objShell.Windows
For Each o In objShellWindows
sURL = ""
On Error Resume Next
'check the URL and if it's the one you want then
' assign it to the return value
sURL = o.document.Location
On Error GoTo 0
'Debug.Print sURL
If sURL Like sLocation & "*" Then
Set retVal = o
Exit For
End If
Next o
Set GetIE = retVal
End Function