How to click a js button in IE using Excel VBA? - excel

I want to create a code in Excel Vba to help me click a js image button in a website.
Here is what i have tried.
Sub test()
Dim ie As Object
Set ie = New InternetExplorer
WebPage = "http://myweb.com" 'a makeup link, not really my site
ie.Visible = True
ie.Navigate WebPage
Application.Wait (Now() + TimeValue("00:00:05"))
ie.Document.getElementById("ctl00_ContentPlaceHolder1_tcSearchRoute_tabSearchRoute2_ibtnSearchR2").Click
End Sub
When I run the code, it says
RunTime Error '-2147217848(80010108)'
Automation error
The object invoked has disconnected from its clients.
Here is the html code for that js image button
<input type="image" name="ctl00$ContentPlaceHolder1$tcSearchRoute$tabSearchRoute2$ibtnSearchR2" id="ctl00_ContentPlaceHolder1_tcSearchRoute_tabSearchRoute2_ibtnSearchR2" src="../Images/button/btn_searchAccount.png" alt="Search Account" style="border-width:0px;">

Your .Click line seems ok to me. Have you been using ie.Quit, before the End Sub, to close off instances each time you run your code? Check how many InternetExplorer instances you have running for starters.
Use a proper page load wait of:
While ie.Busy Or ie.readyState < 4: DoEvents: Wend
Then you could also try
ie.Navigate2 "http://myweb.com" & ie.Document.querySelector("#ctl00_ContentPlaceHolder1_tcSearchRoute_tabSearchRoute2_ibtnSearchR2").src
While ie.Busy Or ie.readyState < 4: DoEvents: Wend
Or just retry you existing line.

You can try to create and set IE object like below.
Set ie = CreateObject("InternetExplorer.Application")
or try
Dim ie As SHDocVw.InternetExplorer
Set ie = New InternetExplorerMedium
or try
Set ie = GetObject("new:{D5E8041D-920F-45e9-B8FB-B1DEB82C6E5E}")
You can try adding one of these wait loops after you set your IE object or anytime the IE page changes/refreshes.
Set ie = CreateObject("InternetExplorer.Application")
Do While ie.readyState <> READYSTATE_COMPLETE
DoEvents
Loop
While ie.busy
DoEvents
Wend
Do While IE.ReadyState = 4: DoEvents: Loop
Do Until IE.ReadyState = 4: DoEvents: Loop
Further, you can try to disable the enable enhanced protected mode option in IE settings.
Go to Tools-> IE options -> Advanced tab -> Security section - disable
"enable enhanced protected mode".
Reference:
VBA Error: The object invoked has disconnected from its clients

Related

getElementsByName "Object variable or With Block Variable not set" Error

The error I'm getting is "Object variable or With Block Variable not set " in reference to the ".click" line of the code. I'm newer to VBA coding and I've tried various different things but I'm unable to figure it out. Any ideas?
The point of the code is to launch a new internet explorer, navigate to a work site, click on a drop down, tab once, then click enter. Ideally I'd like to just click the 'ok' button but I'm getting same error so I'll probably switch it if you guys can tell me what I'm doing wrong to get the error.
Sub GetData()
Dim objIE As InternetExplorer
Dim Ele As Object
Set IE = New InternetExplorer
IE.Visible = True
IE.navigate "http://workurl"
Do While IE.Busy = True Or IE.readyState <> 4: DoEvents: Loop
IE.document.getElementsByName("staffGroupCombo")(0).Click
Application.Wait (Now() + TimeValue("00:00:01"))
SendKeys "+{TAB}"
Application.Wait (Now() + TimeValue("00:00:01"))
SendKeys "+{~}"
'Just placeholder stuff I sometimes need to copy
'Do While IE.Busy = True Or IE.readyState <> 4: DoEvents: Loop
'Debug.Print ele.ClassName
'Debug.Print ele.tagName
'Debug.Print ele.ID
Sometimes constructs in a single line like
IE.document.getElementsByName("staffGroupCombo")(0).Click
don't work. I don't know why.
Try this:
Set Ele = IE.document.getElementsByName("staffGroupCombo")
Ele(0).Click
If the page is not fully loaded like K.Davis write, you can insert a break after the IE says he is ready.
Do While IE.Busy = True Or IE.readyState <> 4: DoEvents: Loop
Application.Wait (Now() + TimeValue("00:00:05"))

Excel VBA - Win10, IE11 - opening second webpage is not working

we've migrated to win10 and IE11and some of my vba macros stopped working. I could resolve some, but I am having issues with resolving following problem.
I have a macro, that opens a webpage in IE. This webpage is opened just to log in to the database. There is a list of customers. Each customer has specific ID which is reflected also in URL. Instead of searching through html code, I am simply entering this customer specific URL to IE. Problem is, that this stopped working on Win 10 IE11. First page (the one used to log in) is opened, but then second URL is not entered and macro gets stucked while it is waiting till that second, customer specific page is loaded. There is also third page which I need to access and its URL is the same for each customer, that's why I need to access log in page first, then customer specific page and then the third page where the list of devices for that customer is. Going directly to device list page is not working. Also, I am not getting any errors or anything like that.
Here is my code:
Dim ie As Object
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
ie.Document.Focus
ie.Navigate "https://webpage_used_to_log_in.com"
While ie.Busy Or ie.ReadyState = READYSTATE_COMPLETE
DoEvents
Wend
ie.Navigate = "https://customer_specific_webpage.com"
While ie.Busy Or ie.ReadyState = READYSTATE_COMPLETE
DoEvents 'the macro stucks here as customer page is never entered and opened
Wend
ie.Navigate = "https://device_list.com"
While ie.Busy Or ie.ReadyState = READYSTATE_COMPLETE
DoEvents
Wend
This was perfectly working in Win7 and IE11. I couldn't find any solution to this issue. If you have any idea how this could be fixed, I'd really appreciate your help. Thank you.
Here is what is working for me:
Dim ie As Object
Set ie = New InternetExplorerMedium 'used this to fix the issue with reading data from html code
ie.Visible = True
ie.Document.Focus
ie.Navigate "https://webpage_used_to_log_in.com"
Do Until.ReadyState = READYSTATE_COMPLETE
DoEvents
Loop
ie.Navigate = "https://customer_specific_webpage.com"
Do Until.ReadyState = READYSTATE_COMPLETE
DoEvents
Loop
ie.Navigate = "https://device_list.com"
Do Until.ReadyState = READYSTATE_COMPLETE
DoEvents
Loop
Thank you all for your help.
Maybe the equal-signs = in the second and third ie.Navigate cause the issue. I think that's a wrong expression. The below code can work well in my IE11 in win10, you could have a try:
Sub LOADIE()
Dim ie As Object
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
ie.Navigate "https://www.google.com"
While ie.Busy Or ie.ReadyState = READYSTATE_COMPLETE
DoEvents
Wend
ie.Navigate "https://www.stackoverflow.com"
While ie.Busy Or ie.ReadyState = READYSTATE_COMPLETE
DoEvents
Wend
ie.Navigate "https://www.bing.com"
While ie.Busy Or ie.ReadyState = READYSTATE_COMPLETE
DoEvents
Wend
End Sub
this can't work:
While ie.Busy Or ie.ReadyState = READYSTATE_COMPLETE
DoEvents
Wend
You set the opposite conditions, that the browser has to be Busy or the ReadyState has to be set to Complete for the loop to continue. So it's an endless loop.
This is how it works:
Sub LOADIE()
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
ie.Navigate "https://www.stackoverflow.com"
Do Until ie.ReadyState = 4: DoEvents: Loop
ie.Navigate "https://www.bing.com"
Do Until ie.ReadyState = 4: DoEvents: Loop
End Sub
All you have to do is check busy or ReadyState. I also wonder why the login page has to be called if there is no login at all.
Best regards
Zwenn

Object htmldivelement doesn't click on IE11

Will go to the school page and click on the like button. I'm going to the page object htmldivelement feature does not click the button. My commands are running with IE10, but it doesn't work with IE11.
Set hIE = CreateObject("InternetExplorer.Application")
hURL = "http://mukerremalikayanilkokulu.meb.k12.tr/icerikler/eglen-dusun-bul_6506730.html"
With hIE
.Navigate hURL
.Visible = True
Do While hIE.Busy
Loop
End With
Set haberss = hIE.document.getElementsByClassName("begen")
For Each haberbb In haberss
If haberbb = "begen" Then
Do While hIE.Busy
Loop
haberbb.Click
Set hIE = hIE.Quit
Exit For
Else
Do While hIE.Busy
Loop
haberbb.Click
Set hIE = hIE.Quit
Exit For
End If
The haberbb button works on IE10, but on IE11 it does not work.
I don’t know if that is functional or needs some form of login. I cannot manually click like.
Here are 3 code ways:
Ie.document.querySelector(".begen").click
Ie.document.querySelector(".begen").FireEvent "OnClick"
Ie.document.parentWindow.execScript "document.querySelector('.begen').click;"
Option Explicit
Public Sub AttemptClick()
Dim ie As New InternetExplorer
With ie
.Visible = True
.Navigate2 "http://mukerremalikayanilkokulu.meb.k12.tr/icerikler/eglen-dusun-bul_6506730.html"
While .Busy Or .readyState < 4: DoEvents: Wend
.document.querySelector(".begen").FireEvent "onclick"
.document.querySelector(".begen").Click
.document.parentWindow.execScript "document.querySelector('.begen').click();"
Stop
.Quit
End With
End Sub
In addition to QHarr's answer, your Do While...Loop isn't quite right.
You should do something in the loop, otherwise you are just going to spike the CPU usage. You should at least add a DoEvents in between to allow windows to process other programs/messages.
It doesn't look like you are waiting for the page load to complete. Try adding Or ReadyState < READYSTATE_COMPLETE to the loop condition.
Putting it together, the loop should look like:
Do While IE.Busy Or IE.ReadyState < READYSTATE_COMPLETE ' = 4
DoEvents
Loop
Here are some other references that might help:
Failproof Wait for IE to load
How to wait for the page to load after click

VBA download won't finish until all code runs

This will be a little vague as i cannot provide all the relevant information needed, but maybe someone is up for the challenge.
I am trying to download an excel file from a website and then save that excel file. Here is my code.
Dim ie As InternetExplorer
Set ie = New InternetExplorer
ie.Visible = True
ie.navigate "####"
Do While ie.Busy = True Or ie.readyState <> 4: DoEvents: Loop
ie.document.getElementById("###").Click
Do While ie.Busy = True Or ie.readyState <> 4: DoEvents: Loop
Application.SendKeys "%{o}"
Set ie = Nothing
ActiveWorkbook.SaveAs "####"
End Sub
The code works when I don't include anything after the sendkeys. If I include any code after the sendkeys the download will wait to download until after that code has been run. I tried using
Application.Wait Not + TimeValue("xx:xx:xx")
but that only served to pause the download as well. Is there any kind of loop that I can run similar to ie readystate loop listed below?
Do While ie.Busy = True Or ie.readyState <> 4: DoEvents: Loop
Or another trick to finish the download before continuing the rest of my macro?

Go to a url and click on a button to download a file

I want to download an Excel file from an url.
Unfortunately, I only have the url that contains the "download button", because once I click the button, the url isn't .../file.xls but one, if I go to, will not activate the download process.
Is there were a way with VBA go to this url and click on the button to download the file.
Dim IE As InternetExplorer
Sub Test()
' Create InternetExplorer Object
Set IE = CreateObject("InternetExplorer.Application")
' You can uncoment Next line To see form results
IE.Visible = True
' Send the form data To URL As POST binary request
IE.navigate "http://webpage.com/"
' Wait while IE loading...
While IE.Busy
DoEvents
Wend
Set objInputs = IE.Document.getElementsByTagName("link")
For Each ele In objInputs
If ele.Title Like "List of control sheets having one or more declared Then alert(s)." Then 'Text on the link
ele.Click
End If
Next
End Sub
However, I think the object I'm aiming at is more like a link than a button, because in the source, there are only several lines of these:
<LINK rel=stylesheet type=text/css href="/frm/wdk/theme/documentum/css/webforms.css">
you can use this to access the page using IE:
Dim ie As Object
Set ie = CreateObject("InternetExplorer.application")
With ie
.Visible = True
.Navigate ("http://yourlink")
While .Busy Or .ReadyState <> 4: DoEvents: Wend
End With
And then use the command SendKeys "{TAB}", True to go to the button and the command SendKeys "{ENTER}", True to click on it

Resources