VBA 对象模型参考文档
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25526335/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VBA Object Model reference documentation
提问by Ayusman
Is there a place where I can simply find the Object Model hierarchy and kind of API for VBA for Excel 2013?
有没有一个地方可以简单地找到对象模型层次结构和 VBA for Excel 2013 的 API 种类?
I am looking at MSDN URL http://msdn.microsoft.com/en-us/library/office/ff841127(v=office.15).aspx
我正在查看 MSDN URL http://msdn.microsoft.com/en-us/library/office/ff841127(v=office.15).aspx
But that seems confusing.
但这似乎令人困惑。
Example:
例子:
If I am writing
如果我在写
Selection.Interior.ColorIndex = -4142
I would like to have an API (online or offline) to understand the Object Models starting with Selection
and work (and explore) my way to ColorIndex. By reading the associated documentation as well.
我想要一个 API(在线或离线)来了解对象模型,Selection
并开始工作(和探索)我的 ColorIndex 方式。通过阅读相关的文档。
The only way I think that's possible is by having a structured document like Java Docs.
我认为这是可能的唯一方法是拥有像 Java Docs 这样的结构化文档。
Any references/tips will help.
任何参考/提示都会有所帮助。
System Info:
MS Excel 2013
VBA
Windows 8, 64 bit
UPDATE: While searching for answers, I got the below image from http://msdn.microsoft.com/en-us/library/aa141044.aspx
更新:在寻找答案时,我从http://msdn.microsoft.com/en-us/library/aa141044.aspx得到了下面的图片
Update:
Based on entry by user Oliver below:
My office help doesn't seem to be working.
更新:根据以下用户 Oliver 的输入:我的办公室帮助似乎不起作用。
回答by Zev Spitz
Generally, if a variable or other identifier can be resolved to a specific type, then Intellisense will show you the members of that type. For example:
通常,如果变量或其他标识符可以解析为特定类型,则 Intellisense 将向您显示该类型的成员。例如:
Application.
will bring up a list of members.
将显示成员列表。
Selection
is a special case because although the currently selected item is often a range of cells, it could be something else as well - part of a chart sheet for example.
Selection
是一种特殊情况,因为尽管当前选定的项目通常是一系列单元格,但它也可能是其他内容 - 例如图表工作表的一部分。
You can discover the underlying type of the object which Selection
refers to, by adding a watch (Debug-> Add Watch...). Type Selection
in the Expressionbox, and set the context to (All Procedures)and (All Modules)if it's not set that way already.
您可以Selection
通过添加监视(Debug-> Add Watch...)来发现所引用对象的底层类型。键入Selection
在表达盒,并设置上下文(所有程序)和(所有模块),如果它不设置这样了。
In the Watcheswindow, you will see the actual type of the object referred to by Selection
, and you can expand the +
to see its properties. If the type says Object/Range
, (meaning the type of the expression is Object
and the type of the object referred to by the expression is Range
), one of the properties will be Interior
. If you look at the type column for Interior
, you'll see Interior/Interior
, because the type of the Interior
property is indeed the Interior
type.
在Watches窗口中,您将看到 引用的对象的实际类型Selection
,您可以展开+
以查看其属性。如果类型为Object/Range
,(意思是表达式Object
的类型是,并且表达式所引用的对象的类型是Range
),那么属性之一就是Interior
。如果您查看 的类型列Interior
,您会看到Interior/Interior
,因为Interior
属性的Interior
类型确实是类型。
If you type the following in code:
如果您在代码中键入以下内容:
ActiveCell.
Intellisense will show you a list of members, including the Interior
property, because the type of the ActiveCell
property is the Range
type.
Intellisense 将向您显示成员列表,包括Interior
属性,因为ActiveCell
属性的Range
类型是类型。
Some other powerful tools for investigating the object model:
其他一些用于研究对象模型的强大工具:
- The Object Browser(View-> Object Browser) shows you a list of types available to your project, based on the libraries which the project references (can be viewed/changed at Tools-> References...). When a type is selected, you will see a list of members for that type. You can filter the types by library and/or by name. Members in the
<globals>
type can be used without any object references --Interior
needs someRange
object to be used, butSelection
can be used by itself. - The Immediate window(View-> Immediate window) lets you evaluate expressions (preface with a
?
-- e.g.?ActiveWorkbook.Sheets.Count
) and run code in place (such asActiveWorkbook.Save
)
- 在对象浏览器(视图- >对象浏览器),你的节目提供给您的项目类型列表,根据该项目引用库(可以看作/改变在工具- >引用...)。选择类型后,您将看到该类型的成员列表。您可以按库和/或按名称过滤类型。
<globals>
类型中的成员可以在没有任何对象引用的情况下使用——Interior
需要一些Range
对象才能使用,但Selection
可以单独使用。 - 在立即窗口(查看- >立即窗口),可以计算表达式(前言用
?
-例如?ActiveWorkbook.Sheets.Count
)和运行代码到位(比如ActiveWorkbook.Save
)
回答by Oliver
The Excel Developer reference is most probably right on your computer, it's just difficult to find:
Excel Developer 参考很可能就在您的计算机上,只是很难找到:
Manual way:
手动方式:
- Start Excel
- Hit Alt-F11 to enter VBA Editor
- On the Menu Bar, choose "?"->Microsoft Visual Basic Help
- 启动 Excel
- 按 Alt-F11 进入 VBA 编辑器
- 在菜单栏上,选择“?”->Microsoft Visual Basic 帮助
Now you are looking at the Excel-VBA Help and you can even search it in the top left box for "Selection".
现在您正在查看 Excel-VBA 帮助,您甚至可以在左上角的框中搜索“选择”。
Direct Link:
直接链接:
"C:\Program Files (x86)\Microsoft Office\Office15\CLVIEW.EXE" "EXCEL.DEV" "Microsoft Office Excel"
(At least this worked for Office 2007, i assume it does for 2013 as well)
(至少这适用于 Office 2007,我认为它也适用于 2013 年)
回答by Krish
Do you mean, while coding you would like to see function/sub explanations like JavaDoc? Unfortunately this is not standard in VBA.
您的意思是,在编码时您希望看到像 JavaDoc 这样的函数/子解释吗?不幸的是,这不是 VBA 的标准。
However you might want to take a look into this Is there a good VB6 documentation system similar to Javadoc?
但是你可能想看看这个 是否有一个类似于 Javadoc 的好的 VB6 文档系统?
回答by tawfiq
Another way to "see" the underlying Object Model called by Excel is to use the macro recorder.
“查看”Excel 调用的底层对象模型的另一种方法是使用宏记录器。
- Open the excel sheet and the VBA environment (ALT+F11) side by side.
- Goto Developer tab and click "Record Macro".
- From the VBE open the module1 from project explorer (this is where exlce writes macro by default).
- Now make changes in your worksheet and see what code Excel is writing.
- 并排打开 Excel 工作表和 VBA 环境 (ALT+F11)。
- 转到“开发人员”选项卡,然后单击“录制宏”。
- 在 VBE 中,从项目资源管理器中打开 module1(这是 exlce 默认写入宏的地方)。
- 现在在您的工作表中进行更改并查看 Excel 正在编写什么代码。