简体   繁体   English

Excel VBA 中的 Application.Windows() 和 Excel.Windows() 有什么区别?

[英]What is the difference between Application.Windows() and Excel.Windows() in Excel VBA?

Basically, when should I use Application.Windows() and when should I use Excel.Windows()?基本上,我什么时候应该使用 Application.Windows(),什么时候应该使用 Excel.Windows()? Any code example would be specifically preferable.任何代码示例都是特别可取的。

Excel.Windows() lets your write to it [1], whereas Application.Windows() is read-only [2] according to the documentation. Excel.Windows()允许您写入它 [1],而Application.Windows()根据文档是只读的 [2]。 [1] , [2] . [1] , [2]

Other than the read-only distinction for the Application version, they are the same.除了Application版本的只读区别之外,它们是相同的。 So, if you're just reading properties, use the Application version, if you need to change something, use the Excel version.因此,如果您只是阅读属性,请使用应用程序版本,如果您需要更改某些内容,请使用Excel版本。

The Excel library has classes named Windows and Application . Excel库具有名为WindowsApplication类。 Then Excel.Windows is the fully qualified name for the class Windows .然后Excel.Windows是类Windows的完全限定名称。 Class Application has property named Windows which returns collection of the windows in all workbooks.Application具有名为Windows属性,它返回所有工作簿中的窗口集合。

So you can use Excel.Windows when you want to refer to a class Windows and you will use Application.Windows property when you want to refer to windows objects.因此,当您要引用类Windows时可以使用Excel.Windows ,而要引用 windows 对象时将使用Application.Windows属性 HTH.哈。

In Object Browser we can see it:在对象浏览器中我们可以看到: 在此处输入图片说明


There is problem with the naming.命名有问题。 The name of class Windows and the property Windows are the same.Windows和属性Windows的名称相同。 All the following code examples refer to the same collection of window objects using the Windows property:以下所有代码示例使用Windows属性引用相同的窗口对象集合:

Dim eaw As Excel.Windows
Dim aw As Excel.Windows
Dim ew As Excel.Windows

Set eaw = Excel.Application.Windows
Set aw = Application.Windows
Set ew = Excel.Windows

The code Set aw = Application.Windows and Set ew = Excel.Windows are the same, because many of the properties and methods that return the most common objects can be used without the Application object qualifier.代码Set aw = Application.WindowsSet ew = Excel.Windows是相同的,因为许多返回最常见对象的属性和方法可以在没有Application对象限定符的情况下使用。 Properties and methods that can be used without the Application object qualifier are considered global and that is the case for Windows as well.可以在没有 Application 对象限定符的情况下使用的属性和方法被认为是global ,对于Windows也是如此。

In summary:总之:

  • When declaring a variable of type Excel.Windows you will not make any mistake when you take the fully qualified name eg Dim wnds As Excel.Windows .在声明Excel.Windows类型的变量时,使用完全限定名称(例如Dim wnds As Excel.Windows

  • When referring to collection of window objects it is up to you which way you choose, all the following are equivalent (return the same collection) Set eaw = Excel.Application.Windows , Set aw = Application.Windows , Set ew = Excel.Windows .当提到窗口对象的集合时,您选择哪种方式取决于您,以下所有内容都是等效的(返回相同的集合) Set eaw = Excel.Application.Windows , Set aw = Application.Windows , Set ew = Excel.Windows . Note: According to Object Browser of my Excel 2007 this collection is read-only .注意:根据我的 Excel 2007 的对象浏览器,这个集合是read-only

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

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