通过Outlook实现IE自动化-拒绝访问问题

时间:2018-06-19 16:56:41

标签: html vba outlook ms-office outlook-vba

Sub test2()
    Dim winObj As Object
    Dim objShell As Object
    Dim objShellWindows As Object
    Dim htmlDoc As HTMLDocument
    Dim frmDoc As HTMLDocument

    Set objShell = CreateObject("Shell.Application")
    Set objShellWindows = objShell.Windows

    For Each winObj In objShellWindows
        If TypeName(winObj.Document) = "HTMLDocument" Then
            Set htmlDoc = winObj.Document
            Set frmDoc = htmlDoc.frames(0).Document
        End If
    Next winObj
End Sub

此代码可在我的2016 Excel,Access,Word和PowerPoint中使用,但在Outlook中失败。 第Set frmDoc = htmlDoc.frames(0).Document行返回错误:

enter image description here

为什么会这样?我的Outlook的MS HTML对象库是否可能已损坏?如何解决?作为参考,以前有一个类似的问题-IE automation via Outlook - Permission Denied issues-现在已被解决。 ,但是我认为这表明我的Outlook可能不正确吗?

编辑(20/06/2018):-有关显示在Excel和Outlook中执行代码的gif图像以及相关站点及其HTML结构的屏幕抓图的信息,请参见https://imgur.com/a/3f5klzP

编辑(23/06/2018):-下面的代码进一步演示了该问题。它单击示例网页上框架内的元素。它可以在Excel,Access等环境中正常运行,但是从Outlook运行时,会在Set frmDoc = htmlDoc.frames("main").document行上触发“访问被拒绝”错误。我要重新发出呼吁-请问有人知道为什么会这样以及如何使其在Outlook中起作用吗?

Sub test3() 'example web page with frames
    Const URL As String = "http://www.easyhtmlcode.com/frames.html"
    Dim IE As New InternetExplorer
    Dim htmlDoc As HTMLDocument
    Dim frmDoc As HTMLDocument

    IE.Visible = True
    IE.navigate URL

    Do While IE.Busy: DoEvents: Loop
    Do Until IE.readyState = 4: DoEvents: Loop

    Set htmlDoc = IE.document
    Set frmDoc = htmlDoc.frames("main").document

'click "Lesson 4: Attributes" link inside the frame

    frmDoc.Body.Children(0).Children(1). _
    Children(1).Children(4).Children(0).Click

End Sub

0 个答案:

没有答案