VBA Internet Explorer Automation 'Permission Denied'

后端 未结 8 1263
别那么骄傲
别那么骄傲 2020-12-19 23:11
Dim IE as New InternetExplorer
IE.Visible = True

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

Do Until IE.Busy = False
Loop

IE.document.getElementsByTagName(\"Input\")(         


        
8条回答
  •  时光说笑
    2020-12-19 23:59

    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

提交回复
热议问题