Table of contents

设置对类型库的引用Set References to Type Libraries

office 365 dev account|上次更新日期: 2018/3/13
2 参与人员

设置到另一个应用程序的类型库的引用时,您可以使用该应用程序代码中提供的对象。例如,如果您在 Excel 库从 Access 设置的引用,然后可以使用自动化 (以前称为 OLE 自动化) 通过 Excel 对象。如果您在另一个 Access 数据库中设置 Visual Basic 项目的引用,您可以调用其公共过程。如果设置 ActiveX 控件的引用,您可以使用 Access 窗体上的该控件。When you set a reference to another application's type library, you can use the objects supplied by that application in your code. For example, if you set a reference from Access to the Excel library, you can then use Excel objects through Automation (formerly called OLE Automation). If you set a reference to a Visual Basic project in another Access database, you can call its public procedures. If you set a reference to an ActiveX control, you can use that control on Access forms.

在 Visual Basic 编辑器中打开,或者您可以在 Visual Basic 代码中设置的引用时,您可以设置的引用进行访问。You can set a reference from Access while the Visual Basic Editor is open, or you can set a reference in Visual Basic code.

设置引用进行访问Setting a Reference from Access

设置对应用程序类型库的引用:To set a reference to an application's type library:

  1. 工具菜单上单击引用。只有模块窗口将打开并在设计视图处于活动状态时,可用工具菜单上的引用命令。On the Tools menu, click References. The References command on the Tools menu is available only when a Module window is open and active in Design view.

  2. 选中要引用其类型库的应用程序复选框。Select the check boxes for those applications whose type libraries you want to reference.

从 Visual Basic 设置引用Setting a Reference from Visual Basic

若要设置 Visual Basic 的引用,您可以创建新引用对象表示所需的参照。引用集合中包含所有当前设置的引用。To set a reference from Visual Basic, you create a new Reference object representing the desired reference. The References collection contains all currently set references.

若要创建新的引用对象,使用AddFromFileAddFromGUID引用集合的方法。若要删除引用对象,请使用Remove方法。To create a new Reference object, use either the AddFromFile or AddFromGUID method of the References collection. To remove a Reference object, use the Remove method.

设置引用的优点Advantages of Setting References

自动化代码将运行得更快,如果您使用它的对象之前设置到另一个应用程序的类型库的引用。如果已经设置的引用,则可以声明对象变量表示为其最具体的类型的其他应用程序中的对象。例如,如果您编写代码来处理 Excel 对象,可使用下面的语法,只有当您创建的 Excel 类型库的引用声明类型Excel.Application的一个对象变量:Your Automation code will run faster if you set a reference to another application's type library before you work with its objects. If you've set a reference, you can declare an object variable representing an object in the other application as its most specific type. For example, if you're writing code to work with Excel objects, you can declare an object variable of type Excel.Application by using the following syntax only if you've created a reference to the Excel type library:

Dim appXL As New Excel.Application

如果您还没有设置 Excel 类型库的引用,您必须为泛型类型为Object的变量声明该变量。下面的代码运行速度更慢:If you haven't set a reference to the Excel type library, you must declare the variable as a generic variable of type Object. The following code runs more slowly:

Dim appXL As Object

此外,如果设置了对某个应用程序的类型库的引用,则它的所有对象,以及对象的方法和属性,都会列在对象浏览器的列表中,这样可以很容易地确定每个对象可以使用哪些属性和方法。Additionally, if you set a reference to an application's type library, all of its objects, as well as their methods and properties, are listed in the Object Browser. This makes it easy to determine what properties and methods are available to each object.

由于访问是支持自动化的 COM 组件,还可以设置从另一个应用程序对其类型库的引用并使用从该应用程序的访问对象。Since Access is an COM component that supports Automation, you can also set a reference to its type library from another application and work with Access objects from that application.

© 2018 Microsoft