VBA Internet Explorer Automation 'Permission Denied'

Dennis Williams picture Dennis Williams · Apr 23, 2012 · Viewed 19.2k times · Source
Dim IE as New InternetExplorer
IE.Visible = True

IE.Navigate("http://www.google.com")

Do Until IE.Busy = False
Loop

IE.document.getElementsByTagName("Input")(3).Value = "Search Term"

IE.document.Forms(0).Submit     <------ This line results in error.

The error states Run-time error 70: 'Permission Denied'

Please do not suggest code alterations. There is NOTHING wrong with the code. This macro works on 9 out of 10 computers. It is NOT a timing issue (I still get the error even if I step through manually). I know there are other ways to declare the internet explorer object. I have tried using CreateObject and all that stuff. None of it matters. Running as administrator does not help either.

This is just a simple example of the problem (we are actually automating much more complex tasks). So please do not ask "why do you want to do a goodle search?" and please do not ask "what are you trying to do". I need this problem solved. I don't need my code re written.

We use Windows XP, Internet Explorer 7, and Office 2003. Something is causing random people to not be able to automate internet explorer. It is not a user issue, but a computer issue. What I mean is on the culprit computers nobody can automate no matter which user logs in. But the same user can use a different computer and everything is fine. Therefore, it is likely a registry setting on the local machine or something like that. All computers are set up the same way here, same specs, same software.

I have googled and googled and googled and googled. Unfortunately run-time error 70 seems to be a catch all and a lot of users report the error for different symptoms. In my case I have not found a solution otherwise I would not be asking here.

The only way we can solve it is to have IT completely reload everything on the hard drive. A clean refresh including the operating system. That takes care of the problem but it also forces the user set their machine up again to the way they had it before and reinstall all of the software and everything. That is not a good solution. There is a setting somewhere on the machine causing this or the refresh would not have an effect. I want to know what that setting is (my feeling is it is a registry setting).

Any help is appreciated, thanks.

Answer

must_improve picture must_improve · Jul 10, 2017

After testing all above suggested fixes and even more, I am positive this is a weird bug with Eventhandling of child Processes invoked by IE whenever there are different security zones on one website.

Cut to the chase, here is the solution:

'Craziest workaround ever due to bugged IE Eventhandling
Do While IE.ReadyState = 4: DoEvents: Loop 
Do Until IE.ReadyState = 4: DoEvents: Loop

Add this code piece every time after navigating to a new page or other events that might cause the website to reload, like clicking on a submit button or similar.

Dan above suggested to add DoEvents statements liberally in your code, which did not 100% do the trick for me. Instead, I suggest to add above code whereever applicable.

Kudos to Dan, without your comment I would've never figured it out!

Short summary of the BENEFITS this method has over other suggested fixes:

  • You can stick to late binding if you wish (Dim IE as Object)
  • You can stick creating a InternetExplorer object (as opposed to i.e. InternetExplorerMedium)
  • You don't need to alter the security zone settings in IE
  • No unnecessary wait times in your application
  • You get rid of Error 70 - Permission denied
  • You get rid of lost sessions i.e. "Run-time error '-2147417848 (80010108)': Automation error The object invoked has disconnected from its clients."
  • You get rid of interface issues i.e. "Run-time error '-2147023179 (800706b5)': Automation error The interface is unknown"

Summary of all the things YOU DO NOT NEED with this approach:

  • InternetExplorerMedium objects
  • Early binding in general
  • Reference to Microsoft Internet Controls
  • Reference to Microsoft Shell Controls and Automation
  • Weird error handling
  • No need to ever check IE.Busy in your code
  • Application.wait or sleep causing unnecessary guesstimated delays

I'm only including above summary because I've seen all those workarounds suggested and have personally tried them with no luck so far.

Example code based on OP's question:

Dim IE as Object    
Set IE = CreateObject("InternetExplorer.Application")

IE.Visible = True
IE.Navigate("http://www.google.com")

'Craziest workaround ever due to bugged IE Eventhandling
Do While IE.ReadyState = 4: DoEvents: Loop
Do Until IE.ReadyState = 4: DoEvents: Loop

IE.document.getElementsByTagName("Input")(3).Value = "Search Term"
IE.document.Forms(0).Submit     ' this previously crashed

I really hope this will help others in the future. Godspeed to everyone who helped with piecing the solution together.

Best Regards Patrick