为什么 VBA 中的每个类都有一个 Application 属性? [英] Why does every class in VBA have an Application property?

查看:33
本文介绍了为什么 VBA 中的每个类都有一个 Application 属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想澄清一下为什么 VBA 中的每个类(或几乎每个类)(当您滚动浏览对象浏览器时)都有一个 Application 属性.

I would like a bit of clarification about why each class (or nearly each class) in VBA (when you scroll through Object Browser) has an Application property.

MSDN 说

在没有对象限定符的情况下使用时,该属性返回一个表示 Microsoft Excel 应用程序的应用程序对象.当与对象限定符一起使用时,此属性返回一个代表指定对象的创建者的应用程序对象(您可以将此属性与 OLE 自动化对象一起使用以返回该对象的应用).只读.

When used without an object qualifier, this property returns an Application object that represents the Microsoft Excel application. When used with an object qualifier, this property returns an Application object that represents the creator of the specified object (you can use this property with an OLE Automation object to return the application of that object). Read-only.

第一句话很清楚.您不带限定符使用的托管应用程序可以是支持 VBA 的 MS Office 产品之一.

The first sentence is pretty clear. Your hosting application used without a qualifier can be one of the MS Office products supporting VBA.

在 Excel 中 Debug.Print Application = Microsoft Excel
在Word中Debug.Print Application = Microsoft Word
等等...

In Excel Debug.Print Application = Microsoft Excel
In Word Debug.Print Application = Microsoft Word
etc...

但是...第二句话对我来说完全不清楚...所以我想无论你从哪里调用 .Application 总是会返回一个对象的主机...

but...the second sentence is totally unclear to me... So I am thinking like no matter where you call .Application from it will always return the host of an object...

  • 那么,VBA 中的每个类都有一个 Application 属性有什么意义?

附注.你总是可以调用 .Parent => .Parent 来提升层次结构......不是吗?你可以只调用 Application 而没有限定符来获得......

PS. you can always call .Parent => .Parent to go up the hierarchy... can't you? You can just call the Application wit no qualifier to get that...

我什至想不出一个 SSCCE OLE 方案,所以很抱歉我没有提供示例.Application 属性(不要Application class 混淆)没有很好的记录,这就是我要求的原因有更多经验的人为我澄清这一点.

I can't even think of one SSCCE OLE scenario for this so I am sorry I am not providing an example. The Application property (not to be confused with Application class) is poorly documented this is why I am asking for someone with more experience to clarify this for me.

   一个(好吧,某种)场景,但不确定它是否适用于此处可能是使用 Interop for Office.就像,当您有一个外部应用程序打开两个文件(一个 Excel 和一个 Word)然后通过获取对两个文件中的 Range 的引用时,您可能能够返回 Microsoft ExcelMicrosoft Word .. 但这对任何人都有用吗?嗯..我是不是不理解整个概念?

    One (well, sort of) scenario, but not sure it even applies here could be using Interop for Office. Like, when you have an external application that opens two files ( one Excel, and one Word ) then by getting references to Ranges in both, you may be able to return Microsoft Excel and Microsoft Word.. but would this be even useful to anyone? hmm..Am I not understanding this entire concept?

推荐答案

这有两个层次.首先,每个类都有一个 Application 属性的原因是因为有人认为这是一个好主意,他们创建了一个基类,每个其他类都继承自该基类.有点像决定通过类一致地应用 Value 属性或 Item 属性.

There are two levels to this. First, the reason every class has an Application property is because somebody thought it would be a good idea and they made a base class from which every other class inherits. Kind of like the decision to have a Value property or an Item property consistently applied through classes.

真正的问题是为什么有人认为这是个好主意.我不知道,但这可能是 Spolsky 的主意,所以他应该回答.我可以告诉你我是如何使用它的.

The real question is why somebody thought it was a good idea. I don't know, but it was probably Spolsky's idea, so he should answer. I can tell you how I use it.

如果我从 Excel 自动化 Outlook,我通常会创建一个 olApp 变量来保存 Outlook 应用程序.但我没有必要.我可以创建一个 MailItem 并通过它引用应用程序.我使用 Application 属性的地方是调试.我实例化了一个 MailItem 对象,但出现错误.例如,即使我在范围内没有应用程序对象变量,我也可以查看收件箱中有多少项目.在即时窗口中:

If I'm automating Outlook from Excel, I'll generally create an olApp variable to hold the Outlook Application. But I don't have to. I could just create, say, a MailItem and reference Application via that. Where I do use the Application property is debugging. I have a MailItem object instantiated and I'm getting an error. I could, for instance, see how many items are in the inbox even though I don't have an application object variable in scope. In the immediate window:

?olMailItem.Application.GetDefaultFolder(1).Items.Count

我可以将一堆 .Parent 调用串在一起,但我不一定知道需要多少个.这是 MSDN 描述的第二部分发挥作用的地方.如果我使用不带限定符的 Application(我在 Excel 中),它默认为 Excel.Application.但是 olMailItem 的 Application 属性返回 Outlook.Application 因为它位于包含 MailItem 类的对象层次结构的顶部.

I could string a bunch of .Parent calls together, but I don't necessarily know how many I would need. Here's where the second part of the MSDN description comes into play. If I use Application with not qualifier (I'm in Excel), it defaults to Excel.Application. But the Application property of olMailItem returns Outlook.Application because that's what's at the top of the object hierarchy that includes the MailItem class.

我认为这只是一个方便的快捷方式,有人认为非常方便,他们将其编码到所有对象的基类中.

I think it's just a handy shortcut that somebody thought was so handy they coded it into the base class for all objects.

这篇关于为什么 VBA 中的每个类都有一个 Application 属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆