简体   繁体   中英

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()? 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. [1] , [2] .

Other than the read-only distinction for the Application version, they are the same. So, if you're just reading properties, use the Application version, if you need to change something, use the Excel version.

The Excel library has classes named Windows and Application . Then Excel.Windows is the fully qualified name for the class Windows . Class Application has property named Windows which returns collection of the windows in all workbooks.

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. 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. All the following code examples refer to the same collection of window objects using the Windows property:

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. Properties and methods that can be used without the Application object qualifier are considered global and that is the case for Windows as well.

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 .

  • 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 . Note: According to Object Browser of my Excel 2007 this collection is read-only .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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