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?
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.
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.
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"))
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 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