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

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

Related

Make the page wait until it loads the second time

I have a site I need to log in and then change the URL. After it loads copy the entire site to excel.
If I'm doing it slowly (F8 button) or do application wait function, it work but its not optimal for different PC, and if I set it too high the data will take forever to load.
I wrote this code:
Set Ie = CreateObject("InternetExplorer.Application")
Ie.Visible = True
Ie.Navigate "Site login"
Do While Ie.Busy = True Or Ie.readyState < READYSTATE_COMPLETE
DoEvents
Loop
Set HTMLDoc = Ie.document
HTMLDoc.getElementById("user_login_name").Value = UserName 'the user name is defined before
HTMLDoc.getElementById("user_password").Value = UserPass 'the password is defined before
LoginButton = Ie.document.getElementsByName("button")
Click = Ie.document.getElementsByTagName
LoginButton.Click
Ie.Navigate "The wanted URL to load fully before the next code activates"
'copy and paste in excel
Ie.ExecWB 17, 0 '// SelectAll
Ie.ExecWB 12, 2 '// Copy selection
Range("A1").Select
ActiveSheet.Paste
Range("A1").Select
Ie.Quit
When I run it now I get the user name from login screen
Anything I'm missing?
Thanks in advance for your time
Nik
Put the code below, after second Ie.Navigate in your VBA code may help to fix your issue.
Do While Ie.Busy = True Or Ie.readyState < READYSTATE_COMPLETE
DoEvents
Loop
Your modified code:
Set Ie = CreateObject("InternetExplorer.Application")
Ie.Visible = True
Ie.Navigate "Site login"
Do While Ie.Busy = True Or Ie.readyState < READYSTATE_COMPLETE
DoEvents
Loop
Set HTMLDoc = Ie.document
HTMLDoc.getElementById("user_login_name").Value = UserName 'the user name is defined before
HTMLDoc.getElementById("user_password").Value = UserPass 'the password is defined before
LoginButton = Ie.document.getElementsByName("button")
Click = Ie.document.getElementsByTagName
LoginButton.Click
Ie.Navigate "The wanted URL to load fully before the next code activates"
Do While Ie.Busy = True Or Ie.readyState < READYSTATE_COMPLETE
DoEvents
Loop
'copy and paste in excel
Ie.ExecWB 17, 0 '// SelectAll
Ie.ExecWB 12, 2 '// Copy selection
Range("A1").Select
ActiveSheet.Paste
Range("A1").Select
Ie.Quit
Output:
Sorry to keep you waiting, I will close this due to still waiting from our users for feedback(they use this files only once a month). which means I can't confirm if my solution was correct or not. However what I did was tweak the code. and added an if to check the current pages link and added the Ie.readyState < READYSTATE_COMPLETE into the code. it looks like the following:
If Ie.LocationURL <> "site link" Then Do While Ie.Busy = True Or Ie.readyState <
READYSTATE_COMPLETE DoEvents Loop
End If
I do this twice. once for the login page, and the second time.
It seems to work for me(did the data scrape at least 10 times and no error)
Thank you for your time and sorry for the wait.

My working macro stopped working (no changes were made)

I have a macro that connects to a web-page, gets some info and displays it on a user form (for further actions):
Private Sub UserForm_Initialize()
Dim login As String, password As String
Dim waittime As String
Dim arrForms As Variant
Dim ie As InternetExplorer
LoadingForm.Show vbModeless
LoadingForm.lblLoading.Caption = "Loading..."
waittime = "0:00:02"
login = "lgn"
password = "passwrd"
txtEndDate.Value = Format(Now(), "dd.mm.yyyy")
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = False
'open login page
ie.navigate "my link (internal company database)"
Do Until Not ie.Busy And ie.readyState = 4
DoEvents
Loop
Application.Wait (Now + TimeValue(waittime))
'if already logged in, skip
On Error GoTo Skip_login
ie.document.getElementById("_58_login").Value = login
ie.document.getElementById("_58_password").Value = password
ie.document.getElementsByClassName("btn btn-primary")(0).Click
Do Until Not ie.Busy And ie.readyState = 4
DoEvents
Loop
Application.Wait (Now + TimeValue(waittime))
Skip_login:
ie.navigate "my link (internal company database)"
Do Until Not ie.Busy And ie.readyState = 4
DoEvents
Loop
Application.Wait (Now + TimeValue(waittime))
arrForms = GetListOfForms(ie) 'this is a function that reads the page,
'finds a table and converts its values to an array
'I tried to comment this line (maybe a bug in my function)
'but even without it the code does not work
For i = LBound(arrForms) To UBound(arrForms)
FormsList.AddItem (arrForms(i))
Next i
'Log out and quit IE
ie.navigate "my link (internal company database)"
Do Until Not ie.Busy And ie.readyState = 4
DoEvents
Loop
Application.Wait (Now + TimeValue(waittime))
ie.document.getElementById("sign-out").Click
Do Until Not ie.Busy And ie.readyState = 4
DoEvents
Loop
Application.Wait (Now + TimeValue(waittime))
ie.Quit
LoadingForm.Hide
End Sub
It worked fine for a few days. It also worked today. But it stopped working for an unknown reason (it just says"automation error" without reference to an error in my code). I was using it, closed the user form, then clicked to start macro again and it does not work. I tried to open older backups - they stopped working too. I did not change anything (links, login/password, any other line of code).
My user form does not have any uncommon user controls. It has the following controls only: text fields, lables, frames, listbox, and buttons)

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

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

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?

ie.navigate2 document does not return second tabs document

Trying to create a vba excel script that opens multiple tabs in IE and populates a textarea.
Something like this:
Sub tab_test()
link1 = "somelink1.com"
link2 = "somelink2.com"
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.navigate link1
While IE.busy Or IE.readyState <> 4: DoEvents: Wend
Set IEdoc = IE.document
For Each tag In IEdoc.getElementsByTagName("textarea")
submit_comment = "Some content No1"
tag.Value = submit_comment
Next tag
IE.Navigate2 link2, 2048
While IE.busy Or IE.readyState <> 4: DoEvents: Wend
Set IEdoc2 = IE.document 'Here is the problem. It does not get the new document, but the first tab
For Each tag In IEdoc2.getElementsByTagName("textarea")
submit_comment = "Some content No2"
tag.Value = submit_comment
Next tag
End Sub
Problem is that when I try to set IEdoc2 to the new document, it sets it to the first one.
IE.Document returns a reference to the active document. You have not set the document in your 2nd tab to be the active document.
You can probably adapt one of the answers at the following link to your needs.
Accessing IE Tabs

Resources