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.
Related
I'm trying to get the content of a web site and paste it onto a excel sheet.
Before trying to code something in VBA I was simply doing "Control + A" (to select ALL the content from the page I want), "Control + C" (to copy it to the clipboard) and then "Control + V" (to paste it in my sheet).
This sort of information is all I want, this is all I need.
The way I will manipulate the data is another story and it's all working just fine.
I need no other way to get information from the web site then the one that "Control + A" and "Control + C" provides me.
That being said, this is the code I've found and which I'm trying to make it work:
Sub Get_Text_From_Page()
Dim sURL As String
sURL = "https://www.microsoft.com"
Dim IE As Object
Set IE = New InternetExplorer
With IE
.Visible = True
.navigate (sURL)
While .Busy Or .ReadyState <> 4: DoEvents: Wend
Range("A1").Value = .Document.body.innerText
End With
End Sub
This opens a browser and then it crashes with an Automation Error at "While .Busy Or .ReadyState <> 4: DoEvents: Wend".
I've found a few code examples in many places, but they all crash with that Busy or ReadyState stuff...
Edit: Not sure if this is gonna take me anywhere, but I've just found something about SendKeys "^a" and then SendKeys "^c".
You could try to refer to the code example below.
It will launch the IE browser. Open the website. After the site gets loaded, it will select all the content, copy it, and paste it to sheet1 in Excel.
Sub demo()
Dim IE As Object
Sheets("Sheet1").Select
Range("A1").Select
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.Navigate "https://microsoft.com"
Do While IE.ReadyState = 4: DoEvents: Loop
Do Until IE.ReadyState = 4: DoEvents: Loop
IE.ExecWB 17, 0
IE.ExecWB 12, 2
Range("A1").Select
ActiveSheet.PasteSpecial Format:="Text", link:=False, DisplayAsIcon:=False
IE.Quit
End Sub
You could try it and let us know whether it works for you.
If you have some questions about the code, please let me know about it.
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
I am trying to Right click on Google Maps page, So far I am able to create the object ie, navigate to the page and search for a particular address.
But in the next step I have to click on the bottom of Address marker (Blue Dot)
After the click, I want to select What's Here from the options.
After the selection, I use the line
ie.document.getElementsByClassName("link-like widget-reveal-card-lat-lng")(0).innerText to get the co-ordinates of the Place.
Code so Far:
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
ie.navigate "https://www.google.com/maps"
Do While ie.Busy
Application.Wait DateAdd("s", 1, Now)
Loop
Application.Wait DateAdd("s", 1, Now)
ie.document.getElementById("searchboxinput").Value = "Signature Tower GurGaon"
ie.document.getElementById("searchbox-searchbutton").Click
MsgBox "After Manually clicking What's Here, press ok"
Basically I want to automate that Manual part. What are the elements that needs to be clicked for the right click and how do I identify the point where to click.
Alternate:
Just found that, those lat-long are also part of the a href by
ie.document.getElementsByClassName("gb_9d gb_2 gb_ob")(0).href
So there is no need to right click.
But I am still interested to know how to Right Click it.
Note that you can put the search term directly into the first navigate.
IE.navigate "https://www.google.com/maps" & "/search/Signature Tower GurGaon"
Your wait loop should check .Busy and .ReadyState
Do While IE.Busy Or IE.ReadyState <> 4
And as you already found out the coordinates are in the source code
Option Explicit
Public Sub Test()
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.navigate "https://www.google.com/maps" & "/search/Signature Tower GurGaon"
Do While IE.Busy Or IE.ReadyState <> 4
Application.Wait DateAdd("s", 1, Now)
Loop
Application.Wait DateAdd("s", 1, Now)
Dim GetLocation As String
GetLocation = IE.document.getElementsByClassName("gb_9d gb_2 gb_ob")(0).href
GetLocation = Right$(GetLocation, Len(GetLocation) - InStr(1, GetLocation, "%40") - 2)
GetLocation = Left$(GetLocation, InStr(1, GetLocation, "&") - 1)
Dim Location() As String
Location = Split(GetLocation, "%2C")
Debug.Print "Lat", Location(0)
Debug.Print "Lon", Location(1)
Debug.Print "zoom", Location(2)
End Sub
Will result in
Lat 28.4636862
Lon 77.054257
zoom 16z
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)
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?