Exception 0x800A01B6 using getElementById after th

2019-03-06 04:49发布

I have created a ribbon for Powerpoint with visual studio XML ribbon. This ribbon has a button that, simplifying, does this:

  • opens an IE browser
  • search an element (hiddenfield) in the code by his id
  • get the value of this element
  • Print the value in the actual slide

It works correctly the first time I click the button of my ribbon, but it throws an Exception 0x800A01B6 the following times I click the button.

This is the code executed when I click the button:

Dim oType As Type = Type.GetTypeFromProgID("InternetExplorer.Application")
If oType IsNot Nothing Then
    Dim ie As SHDocVw.InternetExplorer
    ie = Nothing
    ie = TryCast(Activator.CreateInstance(oType), SHDocVw.InternetExplorer)

    If ie IsNot Nothing Then
        Dim oEmpty As Object = [String].Empty
        Dim oURL As Object = targetURL
        ie.AddressBar = False
        ie.MenuBar = False
        ie.ToolBar = 0
        ie.Visible = True
        ie.Height = 800
        ie.Width = 1100
        ie.Navigate(oURL, oEmpty, oEmpty, oEmpty, oEmpty)
    End If

    Do While (ie.Busy Or ie.ReadyState <> READYSTATE.READYSTATE_COMPLETE)
        Sleep(1000)
        Application.DoEvents()
    Loop

        Sleep(10000) ' 10 seconds for testing purpose

    Dim str As String = String.Empty
    Dim hdnstring As HTMLInputElement = ie.Document.getElementById("hdnstring")
    str = hdnstring.value

    DoSomething(str)

    ie.Quit()
    ie = Nothing
End If

This is the code of the website that opens (targetURL), the code remains identical in every load and only the hidden value changes:

<html>
<body>
    <form name="form1" id="form1">
        <input type="hidden" name="hdnstring" id="hdnstring" value="Get This String" />
    </form>
</body>
</html>

The second time (and following) I execute the function: the IE opens, the website fully loads, it waits 10 seconds and then I get an error in the line:

Dim hdnstring As HTMLInputElement = ie.Document.getElementById("hdnstring")

with Exception 0x800A01B6 message.

The most strange thing is that if I click viewsource in the IE contextual menu while the 10 seconds delay (the ones for testing purpose), it works perfect every time I click the button; but if I don't, the Exception 0x800A01B6 appers.

Any idea of what I'm doing wrong?

Error details image:

screenshoot

1条回答
相关推荐>>
2楼-- · 2019-03-06 05:53

The type of the Document property is only resolved at run-time, so it's an Object until then. This is why calling any methods in it results in the so-called late binding - you do not yet know if the getElementById method exists or not, so that has to be determined a run-time.

You most likely get the error because the Document is not of the IHTMLDocument3 type, which is the only document type that includes the getElementById method.

What you can try is casting the Document to an IHTMLDocument3 interface. Since it inherits IHTMLDocument and IHTMLDocument2 you can cast between them even if the document is actually one of the earlier types.

DirectCast(ie.Document, IHTMLDocument3).getElementById("hdnstring")
查看更多
登录 后发表回答