在 Visual Studio 中比较 VBA 和 Office 解决方案VBA and Office solutions in Visual Studio compared

Microsoft Visual Basic for Applications (VBA) 使用与 Office 应用程序紧密集成的非托管代码。Microsoft Visual Basic for Applications (VBA) uses unmanaged code that is tightly integrated with Office applications. 你可以通过使用 Visual Studio 创建 Microsoft Office 项目以便充分利用 .NET Framework 和 Visual Studio 设计工具。Microsoft Office projects created by using Visual Studio enable you to take advantage of the .NET Framework and Visual Studio design tools.

可以使用 Visual Studio 创建 Office 解决方案的类型的信息,请参阅Office 解决方案开发概述(VSTO)For information about the types of Office solutions you can create by using Visual Studio, see Office solutions development overview (VSTO).

比较Comparison

下表提供了 VBA 解决方案和 Visual Studio 中的 Office 解决方案之间的基本比较。The following table provides a basic comparison between VBA solutions and Office solutions in Visual Studio.

VBA 解决方案VBA solutions Visual Studio 中的 Office 解决方案Office solutions in Visual Studio
使用已连接到特定文档并随之保留的代码。Uses code that is connected to and persisted with a specific document. 使用文档 (对于文档级自定义),分开存储的代码在应用程序 (适用于 VSTO 外接程序) 加载的程序集中。Uses code that is stored separately from the document (for document-level customizations), or in an assembly that is loaded by the application (for VSTO Add-ins).
使用 Office 对象模型和 VBA API。Works with the Office object models and VBA APIs. 提供对 Office 对象模型和 .NET Framework.NET Framework API 的访问权限。Provides access to both the Office object models and the .NET Framework.NET Framework APIs.
专为宏录制而设计,具有更简洁的开发人员体验。Designed for macro recording and a simplified developer experience. 专为安全性、更轻松的代码维护和使用完整的 Visual Studio 集成开发环境 (IDE) 的能力而设计。Designed for security, easier code maintenance, and the ability to use the full Visual Studio integrated development environment (IDE).
适用于从与 Office 应用程序紧密集成中受益的解决方案。Works well for solutions that benefit from a tight integration with Office applications. 适用于从 Visual Studio 和 .NET Framework.NET Framework的完整资源中受益的解决方案。Works well for solutions that benefit from the full resources of Visual Studio and the .NET Framework.NET Framework.
具有针对企业的限制,尤其是在安全性和部署方面。Has limitations for the enterprise, especially in the areas of security and deployment. 设计用于企业中。Designed for use in the enterprise.

使用 VBA 执行某些操作仍可更轻松地快速完成。Some things are still easier to do quickly using VBA. 具体而言,以下操作可能适合继续使用 VBA:Specifically, you might want to continue using VBA for:

  • 自定义工作表函数。Custom worksheet functions.

  • 宏录制。Macro recording.

结合 VBA 解决方案和使用 Visual Studio 创建 Office 解决方案Combine VBA solutions and Office solutions created by using Visual Studio

可以从使用 Visual Studio 创建的 Office 解决方案中调用 VBA 代码,也可以从 VBA 调用使用 Visual Studio 创建的 Office 解决方案中的代码。You can call VBA code from Office solutions created by using Visual Studio, and you can also call code in Office solutions created by using Visual Studio from VBA. 特定方法根据 Office 解决方案是 VSTO 外接程序还是文档级自定义项而有所不同。The specific technique differs depending on whether your Office solution is a VSTO Add-in or a document-level customization. 有关详细信息,请参阅从其他 Office 解决方案调用 VSTO 外接程序中的代码结合 VBA 和文档级自定义项For more information, see Call code in VSTO Add-ins from other Office solutions and Combine VBA and document-level customizations.

请参阅See also