简体   繁体   English

VBA中Internet Explorer窗口的数据类型是什么?

[英]What is the data type of an internet explorer window in VBA?

I was just wondering how do we have to declare "ie" from the following code? 我只是想知道我们如何从以下代码中声明“ ie”?

Dim objShell As Object
Set objShell = CreateObject("Shell.Application")
Set ie = objShell.Windows(x)

Thank you :) 谢谢 :)

You can just use Object but it's easier if you enable the Microsoft Internet Controls library as it creates more specific objects. 您可以只使用Object但是如果启用Microsoft Internet Controls库,它会更容易,因为它会创建更多特定的对象。 This allows you to use intellisense while coding which makes it much less of a guessing game of what properties and actions are available. 这使您可以在编码时使用智能感知,从而使您不必再猜测可用的属性和动作。 Here's some sample code once you have it enabled. 启用后,下面是一些示例代码。

Dim ieobject As InternetExplorer

Set ieobject = New InternetExplorer
ieobject.Visible = True


ieobject.navigate Url:="https://www.StackOverFlow.com"


'get the HTML document for the page.
    Dim ieDOCUMENT As HTMLDocument
    Set ieDOCUMENT = ieobject.document

This site covers it well (along with a lot of other VBA topics) 这个站点很好地介绍了它 (以及许多其他VBA主题)

If you get the error User-defined Type not defined , you haven't enabled the Microsoft Internet Controls library. 如果收到错误“ 用户定义的类型未定义” ,则尚未启用Microsoft Internet Controls库。

在此处输入图片说明

Place a breakpoint (F9) in your procedure, then bring up the Locals toolwindow (View /> Locals) when the breakpoint is hit. 在过程中放置​​一个断点(F9),然后在遇到断点时调出Locals工具窗口(查看/> Locals)。

The runtime data types will appear under the "Type" column 1 : 运行时数据类型将出现在“类型”(Type)列1下

VBE当地人工具窗口

If you are not referencing the type library where these classes and interfaces are defined, the type to use at the declaration site is Object , or Variant - because there is no compile-time reference to the library, the compiler cannot bind the types at compile-time, so you would get a compile error if you tried to declare the object variables otherwise. 如果未引用定义这些类和接口的类型库,则声明站点使用的类型为ObjectVariant因为没有对该库的编译时引用,所以编译器无法在编译时绑定类型-time,因此如果尝试以其他方式声明对象变量,则会出现编译错误。

With a reference to the appropriate type library , you can still use late binding and declare everything As Object , but that would be silly with the library right there waiting to be used. 通过引用适当的类型库 ,您仍然可以使用后期绑定并将所有内容都声明As Object ,但这对于在那里等待使用的库是很愚蠢的。

In your particular example (as provided anyway), the type of ie is... Object , and the reference is Nothing , so expect any member call against ie to throw error 91: 在您的特定示例(无论如何提供)中, ie的类型为... Object ,引用为Nothing ,因此期望对ie任何成员调用均引发错误91:

在此处输入图片说明


1 Unless user interfaces are involved... that's a bug in the VBE, unlikely to ever see a fix. 1除非涉及用户界面,否则这是VBE中的错误,不太可能看到修复程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM