首次加载后使用getElementById的异常0x800A01B6 [英] Exception 0x800A01B6 using getElementById after the first load
问题描述
我已经使用Visual Studio XML功能区为Powerpoint创建了功能区.该功能区具有一个按钮,可以简化此操作:
I have created a ribbon for Powerpoint with visual studio XML ribbon. This ribbon has a button that, simplifying, does this:
- 打开IE浏览器
- 通过他的ID搜索代码中的一个元素(隐藏字段)
- 获取该元素的值
- 在实际幻灯片中打印值
第一次单击功能区的按钮时,它可以正常工作,但是,当我单击该按钮时,它会抛出异常0x800A01B6.
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
这是打开的网站的代码(targetURL),该代码在每次加载时均保持不变,并且只有隐藏值会更改:
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>
第二次(及之后)执行该功能:IE打开,网站完全加载,等待10秒,然后在该行中出现错误:
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")
带有0x800A01B6异常消息.
with Exception 0x800A01B6 message.
最奇怪的是,如果我在IE上下文菜单中单击viewsource同时延迟了10秒(用于测试目的),则每次单击该按钮都可以正常工作;但是如果我不这样做,则异常0x800A01B6会出现.
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?
错误详细信息图像:
推荐答案
Document
属性的类型仅在运行时解析,因此在此之前为Object
.这就是为什么在其中调用任何方法都会导致所谓的后期绑定的原因-您尚不知道getElementById
方法是否存在,因此必须确定运行时.
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.
您很可能会收到错误消息,因为Document
不是 IHTMLDocument3
类型 ,这是唯一包含getElementById
方法的文档类型.
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.
您可以尝试将Document
强制转换为IHTMLDocument3
接口.由于它继承了IHTMLDocument
和IHTMLDocument2
,因此即使文档实际上是较早的类型之一,您也可以在它们之间进行强制转换.
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")
这篇关于首次加载后使用getElementById的异常0x800A01B6的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!