I am having this irritable-IE syndrome while trying to control IE form VBA. I intend to open a website and look for a particular file (i.e. XLS) and then download it.
My Code:
Dim IE as InternetExplorer ' I could also use "Object" here
Dim HTMLDoc as HTMLDocument
Dim OHTML_Element as IHTMLElement
Set IE = New InternetExplorer ' I could also use "InternetExplorerMedium" here
IE.Silent = True
IE.Navigate (sURL)
IE.Visible = True
Set HTMLDoc = IE.document
mimtype = "Microsoft Office Excel 97-2003 Worksheet"
For Each oHTML_Element In HTMLDoc.Links
If InStr(oHTML_Element.mimeType, mimtype) Then
If InStr(oHTML_Element.href, FileType) Then
' Get the SOURCE file and DESTINATION files
SRCfile = mainURL & oHTML_Element.pathname
FileName = DESTPath & oHTML_Element.nameProp
'Download the file
ret = URLDownloadToFile(0, SRCfile, FileName, 0, 0)
'See if the download happened correctly
If ret = 0 Then
'all went well
GetFile = True
Else
MsgBox "There was a problem downloading the file.", vbCritical, "DOWNLOADING ERROR!"
GetFile = False
Exit Function
End If
Exit For
End If
End If
Next oHTML_Element
Now, this seemed to fairly work consistently for the past few months. From past two days, the system hangs after IE.navigate(sURL
. In debugging mode, I can clearly see all the fields of the IE
object that was created but after executing IE.navigate(sURL)
all the fields of the object disappear however the website does open correctly. However, this problem is not very "consistent", so it might disappear or not hence its irritable! This "disappearing" fields is dangerous as further ahead i have a if
condition to check if the fields are empty, if True
to exit.
Just a bit of background, I am on Win 7, IE 11 (upgraded yesterday after this problem on IE 10, previously it was working with IE 10 for months)
After trawling the WWW, it appears some ppl are experiencing this problem. There was as a solution on Stackoverflow suggested to use MSHTML.XMLHTTP60
. So, I tried,
Dim IE as MSHTML2.XMLHTTP60
Dim HTMLDoc as MSHTML.HTMLDocument
Dim HTMLBod as
Set IE = new MSHTML2.XMLHTTP60
IE.open "Get", sURL, False
IE.send
Set HTMLDoc = new MSHTML.HTMLDocument
HTMLDoc.body.innerHtml = IE.responseText
???? How do I proceed further to "search" for a XLS file and gets its "href" link?
I have tried looking at data (via Watch Window) inside HTMLDoc
(using MSHTML) and HTMLDoc
(using InternetExplorer) the data does not seem to be same. So my previous procedure of "search for mime-type & href and download" will not work.
I will really appreciate if someone can throw some light on it?. I am looking for a solution that will either help me rectify the inconsistent behaviour with InternetExplorer
or a solution using MSHTML2
Well... after struggling with it for over 48 hours and trawling the web and finding nothing that would solve my dim IE as InternetExplorer
type object problem, I have "finally" fingers-crossed have solved the IE.Navigate(url)
crashing problem.
This error seems to happen because of some "wrong" setting within the IE browser, you might have changed the setting yourself or some add-on or some other actions might have changed these settings. The almightly "RESET" to the rescue! There is a "RESET" button within IE "Internet Options" dialog, this resets IE to standard settings and all the problems disappeared after I did this!!!