VBA Button pressing on IE explanation - vba

I have managed to get my code to work, but only by searching the internet and finding something that worked, I don't actually understand why.
Could someone please explain why when I used
Dim IE As New InternetExplorer
trying to press a button gave me the error
Object invoked has disconnected from clients
but using
Dim ie As SHDocVw.InternetExplorer
has worked?
Thank You

You are trying to do early binding of IE in VBA. Thus, you need to do it like this:
Dim ie As SHDocVw.InternetExplorer or Dim IE As New InternetExplorer, after adding the "Microsoft Internet Controls" library to the project (c:\windows\syswow64\ieframe.dll).
In general, if you want to do late binding, you should do it like this:
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
What is the difference between Early and Late Binding? and my understanding about late and early binding

Related

Excel VBA - Difference Between CreateObject and New Internet Explorer

Can someone please explain to me if there is a difference between
Set objIE = CreateObject("internetexplorer.application")
and
Set objIE = New InternetExplorer
The first one is late binding, and the second one is early binding. Early binding also requires a reference in the project whereas late binding does not.

How to close a Pop-Up while running RunApplication(X) in VB.NET

I am trying to run an Application, but during the running of this application, there is a pop-up, which makes the code freeze as I do not know how to close the pop-up.
The application is built on VB.NET
Dim oDesk as Object
Dim oApp as Object
oDesk =CreateObject("Desktop.Application")
oApp = oDesk.RunApplication(Input1) 'Reference to the VB.NET code that will navigate to the required module.
SendKeys.SendWait("{ENTER}")
Tried to send ENTER to close the pop-up, but RunApplication gets, stuck. Not sure how to restrict pop-ups etc. in VB.net.
Any help appreciated!
Best,
Gert
You are able typically to create an additional object to handle the popUps. This is rather an application specific, so be careful. The logic depends on the application.
Dim oDesk as Object
Dim oApp as Object
Dim oAppPopUp as Object
oDesk =CreateObject("Desktop.Application")
oApp = oDesk.RunApplication(Input1,oAppPopUp) 'Reference to the VB.NET code that will navigate to the required module.
SendKeys.SendWait("{ENTER}")

Internet Explorer 11 automation VBA Document Empty

I am just starting to learn Internet Explorer Automation using VBA and I am facing an issue.
The Document object is empty which prevents me from going further than just loading a page. I have been looking around the net for a solution to this and could not find anything working so here I am, asking for your help.
NB: My VBA level is intermediate
Windows 8.1 pro 64 bit
I wrote an example code
I had a the same issue (nothing in the .document) , with Internet Explorer 11.
I had to add the site to the Compatibility View list (gear, compatibility view, add)
Hopefully this will help someone avoid the time I spent looking for a solution.
This code works for me. It uses a loop to wait until navigation is complete.
Sub NavigateGoogle()
Dim i As Long
Dim ie As InternetExplorer
Dim Googledoc As HTMLDocument
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
ie.navigate "www.google.com"
Do
DoEvents
Loop While ie.Busy
Set Googledoc = ie.document
Debug.Print Googledoc.all(0).innerHTML
Set ie = Nothing
End Sub

Mark as safe for scripting activeX InternetExplorer object in access with VBA

I try to get the DOM from VBA with access. But I can't get the DOM.
Dim objHTML As Object
With IE
.Navigate2 "http://myanimelist.net/animelist/Alaanor"
WaitIE
Set objHTML = .Document.body.innerHTML
End With
But I have read the documentation of IE.Document and he said:
Warning If the document object type is not marked safe for scripting, this property returns Nothing.
That exactly the problem I have. But I can't find how solve it.
Anyone can help me ?

Automation error when getting ReadyState of InternetExplorer object

I get two different errors on the same line. Sometimes this one:
Automation error: object invoked has disconnected from its clients
and sometimes:
the interface is unknown
Minimal code to reproduce error:
Sub mcve()
Dim ie As Object
Dim www As String
Set ie = New InternetExplorerMedium
www = "http://www.stackoverflow.com"
ie.navigate www
ie.Visible = False
While ie.ReadyState <> 4 ' <~~~~~~~~~~~~~~~~~~~~~~~~ Error occurs here
DoEvents
Wend
End Sub
This requires a reference: Tools > References... > Microsoft Internet Controls
The error occurs on While ie.ReadyState <> 4 the second time. How do I fix this?
This is a duplicate of a previously asked question. The problem seems to be caused by Internet Explorer security settings - when switching between security zones, the current instance of IE is killed and a new instance is created, so your reference to the old process is no longer valid.
Some of the suggested solutions were:
Change IE security settings. Uncheck "enable protected mode" on the Security tab of Internet Options.
Navigate to the IP address directly instead of the URL. This is the one that fixed it for me. For example, ie.navigate "64.233.177.106" (Google's IP address)
Set ie = New InternetExplorerMedium instead of New InternetExplorer. Or in your case, vice versa.
Instead of
Set ie = New InternetExplorerMedium
just use
Set ie = New InternetExplorer
or, for late binding:
Set ie = CreateObject("InternetExplorer.Application")
This makes the error go away.
I'm not sure why you would use InternetExplorerMedium in the first place. Quoting the small print in the documentation:
Remarks
Windows Internet Explorer 8. On Windows Vista, to create an instance of Internet Explorer running at a medium integrity level, pass CLSID_InternetExplorerMedium (defined in exdisp.idl) to CoCreateInstance. The resulting InternetExplorerMedium object supports the same events, methods, and properties as the InternetExplorer object.
Are you really using IE8 on Windows Vista, and do you really want "medium integrity level", whatever that means? I didn't think so.
Open Internet Explorer, then, go to internet settings, open "Sites" ande clear the web page that need the server comprobation. The problema is for the server comprobation.