Excel VBA控制IE本地Intranet

时间:2012-10-18 22:59:45

标签: internet-explorer excel-vba vba excel

所以,我使用Excel VBA代码点击我们网站上的按钮,相信我,我知道这不是最好的事情,但它是目前我可用的最不令人反感的选择。

所以,这个选择的方式,这里是我的问题,我可以使用这个代码,成功加载imdb.com,google等。但是当我加载我们的本地网站时,我失去对ie对象的控制权,我无法检查readyState,我不能退出。这是我得到的错误。 运行时错误'-2147023179(800706b5)':

自动化错误 界面未知

每隔一段时间我就会得到这样的信息: 运行时错误'-2147417848(80010108)':

自动化错误 调用的对象已与其客户端断开连接。

单击Debug指示ie.readyState,我也尝试将其注释掉,然后指向ie.Quit

Sub dothestuff()
    Dim ie As InternetExplorer
    Set ie = New InternetExplorer
    ie.Visible = True

    ie.Navigate "http://www.google.com/"
    anerror = webload(ie)

    ie.Quit
    Set ie = Nothing
End Sub

Function webload(ie)
    Do Until ie.ReadyState = READYSTATE_COMPLETE
        DoEvents
    Loop
End Function

11 个答案:

答案 0 :(得分:27)

以下是此问题的快速简便解决方案:

而不是:

set IE = createobject("internetexplorer.application")

使用:

Set IE = New InternetExplorerMedium

无需调整IE设置

答案 1 :(得分:17)

这里发生了什么。当你的浏览器在内部"跳跃"到不同的安全区域 - 从本地地址到inTRAnet地址或到internet地址 - IE关闭/删除IE的当前进程并打开另一个具有更严格安全性的进程。旧的和新的IE进程是IE子任务的IE子进程,因此您无法通过观察浏览器或在任务管理器中查看进程来查看它。如果您使用Process Explorer(从Microsoft免费),您可以看到这种情况发生。

在此类环境中您需要做的是使用上面的Anup Upadhyay解决方案。他的代码片段查看了所有IE任务(父和子没有区别),并找到指向原始进程所给的Web地址的新IE进程。只要您没有多个浏览器打开同一个地址,它就会可靠地找到新流程,并按照您的预期继续进行。

注意:您可能还需要使用 InternetExplorerMedium 对象而不是 InternetExplorer 对象。在"坚持"参加会议。

这是我的版本,与Anup的版本差不多。

MS Office VBA的参考:

  • Microsoft Internet Controls

  • Microsoft Shell控件和自动化

名词

Dim IE As InternetExplorerMedium ' This object (the "medium" variety as opposed to "InternetExplorer") is necessary in our security climate
Dim targetURL As String
Dim webContent As String
Dim sh
Dim eachIE

targetURL = "[your URL here]"
Set IE = New InternetExplorerMedium
IE.Visible = False ' Set to true to watch what's happening
IE.Navigate targetURL

While IE.Busy
  DoEvents
  Wend

Do
  Set sh = New Shell32.Shell
  For Each eachIE In sh.Windows
    If InStr(1, eachIE.LocationURL, targetURL) Then
      Set IE = eachIE
      'IE.Visible = False  'This is here because in some environments, the new process defaults to Visible.  
      Exit Do
      End If
    Next eachIE
  Loop
Set eachIE = Nothing
Set sh = Nothing

While IE.Busy  ' The new process may still be busy even after you find it
  DoEvents
  Wend

答案 2 :(得分:7)

我们使用IE9遇到了这个问题。它出现是因为安全设置。 “Internet选项”对话框的“安全”选项卡上有一个名为“启用保护模式”的设置(工具...互联网选项)。每个“区域”可以具有“启用保护模式”的不同设置我们取消选中“启用保护模式”,这解决了我们的问题。我认为发生的事情是当切换到处于保护模式的区域时,IE启动一个新进程来处理该区域中的流量(可能会杀死旧进程)。发生这种情况时,VBA中的IE对象变量将与Internet Explorer断开连接。

答案 3 :(得分:7)

试试这个:

Set IE = GetObject("new:{D5E8041D-920F-45e9-B8FB-B1DEB82C6E5E}")

答案 4 :(得分:4)

好吧,刚刚找到了一个解决方案,决定出于绝望尝试加载127.0.0.1而不是localhost,果然没有问题,所以解决了本地内网服务器的ip地址,现在我很高兴去。 我真的不明白为什么,但这解决了我的问题。

答案 5 :(得分:4)

Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = False
IE.Navigate "C:\abc.html"

'我们在这里遇到错误,我已经找到了一个小巧的替代解决方案,如下所示 '参考Microsoft Shell控件和自动化 '使用下面的代码重新分配IE对象以解决丢失的连接问题

Dim sh
Dim eachIE
Do

    Set sh = New Shell32.Shell
    For Each eachIE In sh.Windows
        ' Check if this is the desired URL

' Here you can use your condition except .html
' If you want to use your URL , then put the URL below in the code for condition check.
    ' This code will reassign your IE object with the same reference for navigation and your issue will resolve.
' 
        If InStr(1, eachIE.locationurl, ".html") Then 
        Set IE = eachIE
        Exit Do
        End If
    Next eachIE
Loop

答案 6 :(得分:2)

对我来说,以下工作:

  1. 打开Internet Explorer
  2. 转到工具
  3. 转到互联网选项
  4. 切换到“安全”标签
  5. 单击“可信站点”
  6. 点击网站
  7. 您将看到网站网址,选择它
  8. 点击删除
  9. 点击关闭,应用并确认
  10. 聚苯乙烯。我在IE 8浏览器上测试了这些步骤。

答案 7 :(得分:1)

也许你有绑定问题?这是一个奇怪的错误。

尝试此代码(改编自http://www.excely.com/excel-vba/ie-automation.shtml)。希望它有所帮助:

Option Explicit

' lasts for the life of Excel being open
Dim ie As Object

Sub dothestuff()
    Set ie = CreateObject("InternetExplorer.Application")
    ie.Visible = True
    ie.Navigate "http://www.google.com/"

    Do While ie.Busy
        DoEvents
    Loop

    ie.Quit
    Set ie = Nothing
End Sub

答案 8 :(得分:1)

我在使用公司内部网内部的网站时遇到完全相同的问题,并且也包含在我信任的网站中。禁用保护模式允许我检查ReadyState,但前提是除了Intranet和可信站点之外我还禁用了Internet区域。

为什么在受信任的站点上检查ReadyState时,为Internet区域启用保护模式会导致自动化错误?有没有办法绕过这个没有禁用互联网区域的保护模式?

答案 9 :(得分:1)

另一种解决方案..

Set IE = CreateObject("InternetExplorer.Application")
IE.Navigate ("http://URLOnMyIntranet/site.html")
IE.Visible = True

Set IE = nothing
Set objShellApp = CreateObject("Shell.Application")
For Each objWindow In objShellApp.Windows
    Debug.Print objWindow.LocationName
     If LCase(objWindow.LocationName) = LCase("Your windows name, use debug to find out") Then
            Set IE = objWindow
     End If
Next

注意,使用InternetExplorerMedium(请参阅其他答案)通常只需要你,你也可以使用Url属性obj.LocationURL代替窗口名称,这样会更准确。请参阅其他答案。

答案 10 :(得分:0)

Dim IE As InternetExplorer
Set IE = New InternetExplorerMedium
IE.Navigate "yousite.org"
IE.Visible = True

Do While IE.Busy
    DoEvents
Loop    

Set HTML = IE.document
Do
var8 = HTML.DocumentElement.innerHTML
v3 = var8
v4 = InStrRev(v3, "the class or ID you are looking for when the _    
page final loads", -1)
Loop While v4 = 0

Do While IE.Busy
    DoEvents
Loop 

'Your in the new page look for elements and click rinse and repeat
'the Do loop. This method works with my intranet corporate site