Internet Explorer 11 automation error - vba

I am having a problem when using vba from excel to navigate on multiple pages. I have used the code on ie8 and ie9 without any problems, but in ie11, when i navigate to another url the ie.locationurl and ie.document still refer to the previous url. I noticed this only happens with php websites. I am interested in finding a way to solve this. Any help appreciated.
Here is some example code with a php website:
Sub test()
Dim ie As SHDocVw.InternetExplorer
Set ie = New SHDocVw.InternetExplorer
With ie
.navigate "https://www.facebook.com/" 'php website
.Visible = True
Do While ie.Busy
Loop
Do While ie.readyState <> 4
Loop
.navigate "https://en.wikipedia.org/wiki/Main_Page"
.Visible = True
Debug.Print ie.locationurl
Do While ie.Busy
Loop
Do While ie.readyState <> 4
Loop
End With
End Sub
And here is one that works but doesn't have a php website:
Sub test2()
Dim ie As SHDocVw.InternetExplorer
Set ie = New SHDocVw.InternetExplorer
With ie
.navigate "http://www.break.com/" 'non php website
.Visible = True
Do While ie.Busy
Loop
Do While ie.readyState <> 4
Loop
.navigate "https://en.wikipedia.org/wiki/Main_Page"
.Visible = True
Debug.Print ie.locationurl
Do While ie.Busy
Loop
Do While ie.readyState <> 4
Loop
End With
End Sub

You are requesting the LocationURL property before you've allowed the page to load.
Sub test()
Dim ie As SHDocVw.InternetExplorer
Set ie = New SHDocVw.InternetExplorer
With ie
.Visible = True
.navigate "https://www.facebook.com/" 'php website
Debug.Print "1." & .locationurl
Do While .Busy Or .readyState <> 4: DoEvents: Loop
Debug.Print "2." & ie.locationurl
.navigate "https://en.wikipedia.org/wiki/Main_Page"
Debug.Print "3." & .locationurl
Do While .Busy Or .readyState <> 4: DoEvents: Loop
Debug.Print "4." & .locationurl
End With
End Sub
Results from the VBE's Immediate window:
test
1.
2.https://www.facebook.com/
3.https://www.facebook.com/
4.https://en.wikipedia.org/wiki/Main_Page
I've paired up the ie.Busy and ie.readyState checks and threw DoEvents into the loop to allow some processing to happen while the page loads. You were not taking full advantage of the parent ie object references in the With ... End With statement so I chopped off a few unnecessary ie references within the block.
As the Internet Explorer version is pertinent to this question, this is the version that the code was run on.
            

Related

Query Web Table on Current Website

I am running into a bit of a problem. Normally when I pull a table I use the "data from web" tool in excel, however I now have quite a few places I need to pull data that first require me to enter a username and password. I figured out some code for that (though probably not the most elegant) but realized that once I get to my desired page I have no idea how to extract the table. Here is what I have so far.
Sub Login()
Sheets("IOL").Select
Set ie = CreateObject("InternetExplorer.application")
ie.Visible = True
ie.Navigate ("https://internalsite.company.com/secure/login" & ActiveCell)
Do
If ie.ReadyState = 4 Then
ie.Visible = True
Exit Do
Else
DoEvents
End If
Loop
ie.Document.forms(0).all("badgeBarcodeId").Value = "00000"
ie.Document.forms(0).submit
'used because it redirects to a new page after submitting and I couldn't figure out how to make it wait for the new page to load before proceeding.
Application.Wait (Now + TimeValue("0:00:02"))
ie.Document.forms(0).all("password").Value = "00000"
ie.Document.forms(0).submit
End Sub
After the login is accomplished I would like to go to http://internalsite.company.com/csv and import the csv directly into a sheet. Anytime I make a new connection it makes me log in again so I figure there has to be a way to extract the file without adding a new connection. I'm pretty new with more complex VBA so bear with me.
I was able to get this code to do the job, but it is more preferable to get the CSV directly instead of the table. Sometimes the table doesn't like to load.
Sub Login()
Dim clip As DataObject
Dim ieTable As Object
Set ie = CreateObject("InternetExplorer.application")
ie.Visible = True
ie.Navigate ("https://internalsite1.company.com/secure/login" & ActiveCell)
Do
If ie.ReadyState = 4 Then
ie.Visible = True
Exit Do
Else
DoEvents
End If
Loop
ie.Document.forms(0).all("badgeBarcodeId").Value = "00000"
ie.Document.forms(0).submit
Do While ie.Busy: DoEvents: Loop
Do Until ie.ReadyState = 4: DoEvents: Loop
ie.Document.forms(0).all("password").Value = "000000"
ie.Document.forms(0).submit
Do While ie.Busy: DoEvents: Loop
Do Until ie.ReadyState = 4: DoEvents: Loop
ie.Navigate "http://internalsite2.company.com/site/Inbound?filter=1To3Days"
Do While ie.Busy: DoEvents: Loop
Do Until ie.ReadyState = 4: DoEvents: Loop
Set ieTable = ie.Document.all.Item("DataTables_Table_0")
If Not ieTable Is Nothing Then
Set clip = New DataObject
clip.SetText "" & ieTable.outerHTML & ""
clip.PutInClipboard
Workbooks("Production Meeting Dashboard.xlsm").Activate
Sheets("IOL").Select
Range("A1").Select
ActiveSheet.PasteSpecial Format:="Unicode Text", link:=False, _
DisplayAsIcon:=False, NoHTMLFormatting:=True
End If
End Sub

Not able to login to a webpage using excel, error 424 object required

I am trying to login to this webpage, https://www.fois.indianrail.gov.in/ecustomer/JSP/QryInsight.jsp
using VBA. Debugging shows me that the VBA throws an error 424 object required when username line is active (apparently it is not able to fill the username data).
Here's the code:
Sub Test()
Set ie = CreateObject("InternetExplorer.application")
ie.Visible = True
ie.Navigate ("https://www.fois.indianrail.gov.in/ecustomer/JSP/QryInsight.jsp")
With ie.document
.getElementById("txtUserId").Value = "ABCDE"
.getElementById("txtPassword").Value = "ABCDE"
.getElementById("submit").Click
End With
End Sub
Can anyone help me with debugging the problem while logging in to the given webpage?
Take a look at the below example:
Option Explicit
Sub Test()
Dim oIE As Object
Set oIE = CreateObject("InternetExplorer.application")
With oIE
.Visible = True
.Navigate ("https://www.fois.indianrail.gov.in/ecustomer/JSP/QryInsight.jsp")
Do While .ReadyState < 4 Or .Busy
DoEvents
Loop
With .Document
Do While .ReadyState <> "complete"
DoEvents
Loop
With .parentWindow.frames("frmCUMain").document
.getElementsByName("txtUserId")(0).Value = "ABCDE"
.getElementsByName("txtPassword")(0).Value = "ABCDE"
.getElementsByName("cmdLogin")(0).Click
End With
End With
End With
End Sub

Reference Cell in VBA Script

I was curious if there is a way to reference a specific cell within the following VBA script. The cell contains a date in yyyymmdd format, which would go in the commented section below.
Sub OpenData()
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = False
IE.navigate "http://website.com/'desired cell value'/subdirectory/file.txt"
'Check for good connection to web page loop!
Do
If IE.readyState = 4 Then
IE.Visible = True
Exit Do
Else
DoEvents
End If
Loop
'Wait for window to open!
Application.wait (Now + TimeValue("0:00:02"))
'MsgBox "Done"
IE.Visible = True
Eric
Have you tried format?
'...
IE.navigate "http://website.com/" & Format (Cells(1,1).Value, “dd/MM/yyyy”) & "/subdirectory/file.txt" '...

"Run-time automation error -2147417848 (80010108)"

I use VBA excel to parse a long list of local .htm files. The problem is that I get an error even before the programm starts to parse the HTM-files.
Error is:
VBA code:
<!-- language: lang-html -->
Enum READYSTATE
READYSTATE_UNINITIALIZED = 0
READYSTATE_LOADING = 1
READYSTATE_LOADED = 2
READYSTATE_INTERACTIVE = 3
READYSTATE_COMPLETE = 4
End Enum
Sub ImportHTM()
'Dim ie As InternetExplorer
Dim ie As InternetExplorerMedium
Dim html As HTMLDocument
Set ie = New InternetExplorerMedium
'Set ie = New InternetExplorer
ie.Visible = False
ie.navigate "d:\Cloud\Dropbox\3.htm"
Do While ie.READYSTATE <> READYSTATE_COMPLETE
Application.StatusBar = "Loading Profile..." 'PROBLEM SEEMS TO BE HERE SOMEWHERE!
DoEvents
Loop
Set html = ie.document
Set ie = Nothing
Application.StatusBar = ""
'code code code --> which at this point isn't executed because the error occures before
Do you have any ideas what could cause the problem? Do you have any solution suggestions?
Also the command:
ie.Visible = False
doesn't seem to have any effect whatsoever since it opens the HTM-file in a new IE window.
Move the status bar update out of the loop.
Application.StatusBar = "Loading Profile..."
Do While ie.Busy Or ie.READYSTATE <> READYSTATE_COMPLETE
DoEvents
Loop
Application.StatusBar = vbNullString
There is no need to rewrite the same message into the Application.StatusBar property hundreds if not thousands of times while you are waiting on a page load.
Regarding a new Internet.Explorer 'window' not inheriting the .Visible = False attribute, I recommend you switch to .Navigate2 and ShellWindows.
Addendum: Don't destroy your ie object until you are finished with the html associated with ie.document.

I can not get Web site text using VBA IE

I used the VBA below and monitored the variable IE.document. When the url is IE.navigate="http://www.mixi.jp", I could get all the web text using IE.document.all.
But in other sites like "http://www.yahoo.co.jp" , I could not get Web text. Why is that?
Sub Main()
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.navigate "http://www.yahoo.co.jp"
Do While IE.Busy Or IE.readyState < 4
DoEvents
Loop
End Sub
Because this particular website's IE.document is nothing:
This seems to be the case with dynamically served sites, there are some suggestions here which I use below.
Although I am not sure you will be able to easily "get all of the text", you can certainly still iterate over the elements you're interested in extracting:
Sub Main()
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
ie.navigate "http://www.yahoo.co.jp"
Do While ie.Busy Or ie.readyState < 4
DoEvents
Loop
'Create a collection of DIV elements for example
Dim myElements
DIm ele
Set myElements = ie.Document.Body.GetElementsByTagName("DIV")
'Then you can iterate over the DIV elements/etc., modify as needed.
For each ele in myElements
Debug.Print ele.InnerText
Next
End Sub