VBA Internet Explorer 自动化“权限被拒绝”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10283496/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 15:54:15  来源:igfitidea点击:

VBA Internet Explorer Automation 'Permission Denied'

vbainternet-explorer-7automationpermission-deniedshdocvw

提问by Dennis Williams

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'

错误状态运行时错误 70:“权限被拒绝”

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.

请不要建议更改代码。代码没有任何问题。此宏适用于 10 台计算机中的 9 台。这不是时间问题(即使我手动执行,我仍然会收到错误消息)。我知道还有其他方法可以声明 Internet Explorer 对象。我试过使用 CreateObject 和所有这些东西。这都不重要。以管理员身份运行也无济于事。

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.

这只是问题的一个简单示例(我们实际上正在自动化更复杂的任务)。所以请不要问“为什么要进行goodle搜索?” 请不要问“你想做什么”。我需要解决这个问题。我不需要重新编写我的代码。

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.

我们使用 Windows XP、Internet Explorer 7 和 Office 2003。某些原因导致随机人员无法自动化 Internet Explorer。不是用户问题,是电脑问题。我的意思是在罪魁祸首的计算机上,无论哪个用户登录,都没有人可以自动化。但同一个用户可以使用不同的计算机,一切都很好。因此,它可能是本地计算机上的注册表设置或类似的设置。这里所有的电脑都以相同的方式设置,相同的规格,相同的软件。

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.

我用谷歌搜索和谷歌搜索,谷歌搜索和谷歌搜索。不幸的是,运行时错误 70 似乎是一个包罗万象的问题,许多用户报告了不同症状的错误。就我而言,我还没有找到解决方案,否则我不会在这里问。

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).

我们可以解决的唯一方法是让 IT 完全重新加载硬盘驱动器上的所有内容。干净的刷新,包括操作系统。这解决了问题,但也迫使用户将他们的机器重新设置为以前的方式,并重新安装所有软件和所有内容。这不是一个好的解决方案。机器上某处有一个设置会导致这种情况,否则刷新不会产生影响。我想知道那个设置是什么(我的感觉是它是一个注册表设置)。

Any help is appreciated, thanks.

任何帮助表示赞赏,谢谢。

回答by must_improve

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.

在测试了上述所有建议的修复甚至更多之后,我肯定这是一个奇怪的错误,当一个网站上有不同的安全区域时,IE 会调用子进程的事件处理。

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.

上面的 Dan 建议在你的代码中自由添加 DoEvents 语句,这对我来说并没有 100% 成功。相反,我建议在适用的地方添加上面的代码。

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"
  • 如果你愿意,你可以坚持后期绑定(将 IE 作为对象)
  • 您可以坚持创建 InternetExplorer 对象(而不是 InternetExplorerMedium)
  • 您不需要更改 IE 中的安全区域设置
  • 在您的应用程序中没有不必要的等待时间
  • 你摆脱了错误 70 - 权限被拒绝
  • 您摆脱了丢失的会话,即“运行时错误'-2147417848(80010108)':自动化错误调用的对象已与其客户端断开连接。”
  • 您摆脱了接口问题,即“运行时错误'-2147023179(800706b5)':自动化错误接口未知”

Summary of all the things YOU DO NOT NEEDwith 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
  • InternetExplorerMedium 对象
  • 一般早期绑定
  • 参考 Microsoft Internet Controls
  • 参考 Microsoft Shell 控件和自动化
  • 奇怪的错误处理
  • 无需在代码中检查 IE.Busy
  • Application.wait 或 sleep 导致不必要的猜测延迟

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:

基于OP问题的示例代码:

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

最好的问候帕特里克

回答by Justin Valle

I am receiving the same error when automating IE, but instead of fixing, I worked around.

我在自动化 IE 时收到同样的错误,但我没有修复,而是解决了。

  Label1:
         If myIe Is Nothing Then
             Set myIe = CreateObject("InternetExplorer.Application") 
         End If

         myIe.Navigate URL:=imdbLink

    For Each link In myIe.document.Links
         If Right(link.href, 9) = "/keywords" And Left(link.href, 26) = "http://www.imdb.com/title/" Then
                    mKPlot = link.href 'ERROR GENERATES FROM THIS, SOMETIMES...
         End If
    next link



errHandler:
    If Err.Number = 70 Then
        Debug.Print "permission denied for: |" & title & "|"

        myIe.Quit
        Set myIe = Nothing

        Application.Wait Now + TimeValue("00:00:10")

        Resume Label1:

    End If

Not the sleek registry-fix you were imagining, but it works for me.

不是您想象的时尚注册表修复,但它对我有用。

回答by Francis

I was having this exact same problem. Had a script for automating IE that had been working fine for a year. Then out of the blue I started receiving this "permission denied error". I did two things, one of which fixed the problem, but I don't know which one.

我遇到了完全相同的问题。有一个自动化 IE 的脚本,该脚本已经运行了一年。然后突然间我开始收到这个“权限被拒绝错误”。我做了两件事,其中一件解决了问题,但我不知道是哪一件。

  1. I removed the reference to "Microsoft Internet Controls", attempted to compile the project, added the reference to "Internet Controls" back in, and compiled the project.

  2. Removed the 'WithEvents' keyword when declaring the InternetExplorer object since I did not need to capture events events from IE anyway.

  1. 我删除了对“Microsoft Internet Controls”的引用,尝试编译该项目,重新添加对“Internet Controls”的引用,然后编译该项目。

  2. 在声明 InternetExplorer 对象时删除了 'WithEvents' 关键字,因为无论如何我都不需要从 IE 捕获事件事件。

I suspect it was the first and that somehow the reference became corrupted and removing at and re-adding it fixed the problem.

我怀疑这是第一个,并且以某种方式引用被损坏并删除并重新添加它解决了问题。

回答by Justin

This is a common issue with Internet Explorer automation in VBA. It generally seems to happen after a new page has been loaded. I have had success using a combination of DoEvents, Application.Wait, and setting my HTMLDocument object to Nothing after a new page load.

这是 VBA 中 Internet Explorer 自动化的常见问题。它通常似乎发生在加载新页面之后。我已经成功地使用了 DoEvents、Application.Wait 和在新页面加载后将我的 HTMLDocument 对象设置为 Nothing 的组合。

I have posed sample code below to demonstrate:

我在下面提出了示例代码来演示:

Option Explicit

Sub IEAutomation()

    Dim IE As InternetExplorer
    Dim html As HTMLDocument
    Dim i As HTMLHtmlElement
    Dim search_bar_id As String
    Dim submit_button_name As String
    Dim search_term As String

    'Define Variables
    search_bar_id = "lst-ib"
    submit_button_name = "btnK"
    search_term = "Learning VBA"

    'Create IE and Naviagate to Google
    Set IE = CreateObject("InternetExplorer.Application") 'Late Binding
    IE.Visible = True
    IE.navigate "http://www.google.com"

    'Wait for IE To Load
    Do While IE.readyState <> READYSTATE_COMPLETE: DoEvents: Loop

    'Create HTMLDocument Object
    Set html = IE.document

    'Enter Search Term into Search Bar and Click Submit Button
    html.getElementById(search_bar_id).innerText = search_term
    Application.Wait Now + TimeValue("0:00:01")
    html.getElementsByName(submit_button_name)(, 1).Click

    'Wait for New Page to Load (** DoEvents Helps with the Permission Issue **)
    Do While IE.readyState <> READYSTATE_COMPLETE: DoEvents: Loop

    'After Page Loads, Reset HTMLDocument Object and Wait 1 Second Before Recreating It
    Set html = Nothing
    Application.Wait Now + TimeValue("0:00:01") '<<< This seems to really help
    Set html = IE.document

    'Print All Elements In H3 Tags to Immediate Window in VBE
    For Each i In html.getElementsByTagName("H3")
        Debug.Print i.innerText
    Next i

End Sub

回答by Asger

The solution of resetting the HTMLDocument Object works for me too i.e.

重置 HTMLDocument 对象的解决方案也适用于我,即

'After Page Loads, Reset HTMLDocument Object and Wait 1 Second Before recreating It
Set html = Nothing
Application.Wait Now + TimeValue("0:00:01") '<<< This seems to really help
Set html = IE.document

None of the other solutions have solved my problem.

其他解决方案都没有解决我的问题。

回答by yyc123

The following worked for me. I haven't tested it extensively, but the problem hasn't recurred. I was getting the error intermittently, but always at same point.

以下对我有用。我没有对其进行广泛的测试,但问题没有再次出现。我间歇性地收到错误,但总是在同一点。

Control of the first IE instance seems to die. However, another instance can be created by referencing the first. The dead instance can then be closed, and we carry on.

控制第一个 IE 实例似乎不行了。但是,可以通过引用第一个实例来创建另一个实例。然后可以关闭死的实例,我们继续。

Set IEa = CreateObject("InternetExplorer.Application")
Set IEa = IE
   'Where IE is the instance that's about to error
SendKeys ("%{F4}")
   'Closes the old window, IE.quit in the code wouldn't work
IEa.Visible = True

Hope this works for someone else, I was getting pretty frustrated.

希望这对其他人有用,我感到非常沮丧。

Brian

布赖恩

PS: My first post on stackoverflow, I think I did the formatting right

PS:我在stackoverflow上的第一篇文章,我认为我的格式正确

回答by Dan

I had the same Run time error 70. Permission denied when automating a Webbrowser control in VBA under MS Access 2010. My objective was to set innerHTML for a Content Editable node. Initially, it worked perfectly and then for an unknown reason, I started getting "Error 70. Permission Denied." Interestingly, this error seems to come from the IE Web Browser control and not from MS Access. At times, it was not trapped by the VBA error handler, On Error. Another puzzle is that when single stepping through the code, it would work, but when running normally, the Webbrowser control would just not function, but not raise an error. However, it would raise an error if I set a break point and then queried the WebBrowser in the immediate pane on VBA with code such as: ? DIV.innerHTML

我有同样的运行时错误 70。在 MS Access 2010 下在 VBA 中自动化 Webbrowser 控件时权限被拒绝。我的目标是为内容可编辑节点设置 innerHTML。最初,它运行良好,然后由于未知原因,我开始收到“错误 70。权限被拒绝”。有趣的是,这个错误似乎来自 IE Web 浏览器控件,而不是来自 MS Access。有时,它不会被 VBA 错误处理程序 On Error 捕获。另一个难题是,当单步执行代码时,它可以工作,但正常运行时,Webbrowser 控件将不起作用,但不会引发错误。但是,如果我设置断点,然后在 VBA 的直接窗格中使用以下代码查询 WebBrowser,则会引发错误:DIV.innerHTML

Here is the code that failed:

这是失败的代码:

Private Function SetInnerHTML(HTML As String) As String
Dim HTMLEmail As HTMLDocument
Dim DIV As HTMLDivElement
Set HTMLEmail = Me.Email_MainBrowser.Controls("MyBrowser").Object.Document
Set DIV = HTMLEmail.getElementById("MyText")
DIV.innerHTML = HTML ' This is the line that failed

Like Dennis, the OP, I tried adding in Sleep calls and added in function calls to make sure the Browser returned "Ready State" CurrentState = Me.Form("Email_MainBrowser").Controls("MyBrowser").ReadyState (CurrentState should be acComplete).

像 OP 丹尼斯一样,我尝试添加睡眠调用并添加函数调用以确保浏览器返回“就绪状态” CurrentState = Me.Form("Email_MainBrowser").Controls("MyBrowser").ReadyState (CurrentState 应该是完成)。

Here are the things I tried which did not solve the problem: 1) Decompile and recompile the MS Access application. 2) Add calls to Win32 sleep function to wait for the WebBrowser control 3) Queried the Webbrowser to make sure it was in a Navigation Complete state.

以下是我尝试过但没有解决问题的方法: 1) 反编译并重新编译 MS Access 应用程序。2) 添加对 Win32 睡眠功能的调用以等待 WebBrowser 控件 3) 查询 Webbrowser 以确保它处于导航完成状态。

And here is what fixed it: Added "DoEvents" liberally around the code referencing and automating the webbrowser control:

这是修复它的原因:在代码引用和自动化 webbrowser 控件周围自由添加了“DoEvents”:

Dim HTMLEmail As HTMLDocument
Dim DIV As HTMLDivElement
DoEvents
Set HTMLEmail = Me.Email_MainBrowser.Controls("MyBrowser").Object.Document
Set DIV = HTMLEmail.getElementById("MyText")
DoEvents
DIV.innerHTML = HTML
DoEvents

I have not worked out the minimum number of DoEvents needed; it is probably just one strategically placed. I also still check to make sure the webbrowser control is in the Natigation complete state, but I'm sure this is a bug deep in the Webbrowser control. One that will probably never be fixed.

我还没有计算出所需的最小 DoEvent 数量;它可能只是一个战略位置。我还在检查以确保 webbrowser 控件处于 Natigation 完成状态,但我确定这是 Webbrowser 控件中的一个错误。一个可能永远不会被修复的。

回答by Siddharth Rout

Try this code for me. This bypasses the need to select/click any element in the browser.

为我试试这个代码。这绕过了在浏览器中选择/单击任何元素的需要。

I tested this for INDIA

我为印度测试了这个

Sub Sample1() '<~~ Works as expected
    Dim IE As New InternetExplorer
    IE.Visible = True
    IE.Navigate ("http://www.google.co.in/#hl=en&q=" & "Search Term")
End Sub

For UK

对于英国

Sub Sample2() '<~~ Works as expected
    Dim IE As New InternetExplorer
    IE.Visible = True
    IE.Navigate ("http://www.google.co.uk/#hl=en&q=" & "Search Term")
End Sub

Try this for US (Couldn't Test It as it redirects my request)

为美国试试这个(无法测试它,因为它重定向了我的请求)

Sub Sample3()
    Dim IE As New InternetExplorer
    IE.Visible = True
    IE.Navigate ("http://www.google.com/#hl=en&q=" & "Search Term")
End Sub