# Visual Basic for Applications

**Visual Basic for Applications**（VBA）是微软 [Visual Basic](https://www.edgepedia.org/zh/visual-basic) 语言的一种实现，内置于 [Microsoft Office](https://www.edgepedia.org/zh/microsoft-office) 应用程序中，并授权给其他软件厂商使用，用于编写宏和加载项，以自动化和扩展宿主程序。<sup>[1](https://help.libreoffice.org/latest/en-US/text/sbasic/shared/vbasupport.html?DbPAR=WRITER&System=UNIX)</sup><sup> • </sup><sup>[2](https://news.microsoft.com/source/1999/04/28/microsoft-releases-visual-basic-for-applications-version-6-0-to-software-vendors/)</sup> 用户在 Office 应用程序中用鼠标、键盘或对话框所能执行的几乎每一项操作，也都可以通过 VBA 代码来完成；而在代码中做过一次的事情，可以根据需要重复任意多次。<sup>[3](https://learn.microsoft.com/en-us/office/vba/library-reference/concepts/getting-started-with-vba-in-office)</sup>

| 关键事实 | 详情 |
|---|---|
| 首次发布 | 1993 年在 Microsoft Excel 中实现<sup>[4](https://news.microsoft.com/source/1996/06/04/microsoft-announces-widespread-licensing-and-availability-of-visual-basic-applications-edition/?ver=3.7.1)</sup> |
| 版本历史 | VBA 5.0 于 1996 年广泛授权；VBA 6.0 于 1999 年 4 月 28 日发布给厂商；VBA 7 随 64 位 Office 2010 推出<sup>[4](https://news.microsoft.com/source/1996/06/04/microsoft-announces-widespread-licensing-and-availability-of-visual-basic-applications-edition/?ver=3.7.1)</sup><sup> • </sup><sup>[2](https://news.microsoft.com/source/1999/04/28/microsoft-releases-visual-basic-for-applications-version-6-0-to-software-vendors/)</sup><sup> • </sup><sup>[5](https://learn.microsoft.com/en-us/office/vba/language/concepts/getting-started/64-bit-visual-basic-for-applications-overview)</sup> |
| 编程模型 | 每个宿主都公开一个对象模型：带有属性（设置）和方法（操作）的对象，按层次结构组织，顶端是 Application 对象<sup>[3](https://learn.microsoft.com/en-us/office/vba/library-reference/concepts/getting-started-with-vba-in-office)</sup> |
| 宏文件格式 | .docm 和 .dotm（Word）、.xlsm、.xltm、.xlam（Excel）、.pptm、.potm、.ppam、.ppsm（PowerPoint）；默认的 .docx、.xlsx 和 .pptx 格式无法存储 VBA 代码<sup>[6](https://learn.microsoft.com/cs-cz/office/vba/library-reference/concepts/security-notes-for-microsoft-office-solution-developers)</sup><sup> • </sup><sup>[7](https://learn.microsoft.com/en-us/office/compatibility/office-file-format-reference)</sup> |
| 默认安全设置 | 来自互联网的文件中的宏默认被阻止；用户会看到“安全风险”横幅，必须显式解除阻止或信任该文件<sup>[8](https://learn.microsoft.com/en-us/microsoft-365-apps/security/internet-macros-blocked)</sup> |
| 平台覆盖 | 在 Windows 和 Mac 的 Excel 以及其他桌面 Office 产品中运行；不在 Web 版 Excel 或 iOS 版 Excel 中运行<sup>[9](https://learn.microsoft.com/en-us/office/dev/scripts/resources/vba-differences)</sup> |
| 文件格式规范 | 微软公开了 Office 97 及更高版本的 VBA 文件格式结构（MS-OVBA）<sup>[10](https://learn.microsoft.com/en-us/openspecs/office_file_formats/ms-ovba/575462ba-bf67-4190-9fac-c275523c75fc)</sup> |

## 用途与编程模型

VBA 的存在是为了让最终用户自动化宿主应用程序中的重复性工作：清理 Word 中的五十个表格、在文档打开时提示输入，或者把联系人从 Outlook 移到 Excel 中。<sup>[3](https://learn.microsoft.com/en-us/office/vba/library-reference/concepts/getting-started-with-vba-in-office)</sup> 代码针对一个已经打开、对象处于活动状态的应用程序运行，例如 Application 本身、Excel 中的 Worksheet、Word 中的 Document、PowerPoint 中的 [Presentation](https://www.edgepedia.org/zh/presentation-program)，或者 Outlook 中的 Explorer 和 Folder 对象。<sup>[3](https://learn.microsoft.com/en-us/office/vba/library-reference/concepts/getting-started-with-vba-in-office)</sup>

每个应用程序都将其可编程组件组织为一个对象模型，即一个大致对应用户界面的对象层次结构。在 Word 中，顶层的 Application 对象包含 Document 对象，Document 对象又包含 [Paragraph](https://www.edgepedia.org/zh/paragraph) 对象，依此类推。类是创建对象的模板。对象一旦存在，代码便通过设置属性（改变对象外观或行为的特性）和调用方法（使对象执行操作）来操纵它。<sup>[3](https://learn.microsoft.com/en-us/office/vba/library-reference/concepts/getting-started-with-vba-in-office)</sup> 事件是对象能够识别的动作，例如鼠标单击或按键，可以编写代码来响应；事件可以来自用户操作、程序代码或系统。<sup>[11](https://learn.microsoft.com/en-us/office/vba/language/concepts/getting-started/understanding-objects-properties-methods-and-events)</sup>

一个典型的两行 Excel 示例展示了这一模式：`Application.ActiveSheet.Range("A1").Select` 定义一个覆盖单元格 A1 的 Range 并将其选中，`Application.Selection.Value = "Hello World"` 则设置所得 Selection 的 Value 属性，将文本放入该单元格。<sup>[3](https://learn.microsoft.com/en-us/office/vba/library-reference/concepts/getting-started-with-vba-in-office)</sup> 各 Office 应用程序的对象模型彼此相似，但各自特定于其所处理的文档类型。<sup>[3](https://learn.microsoft.com/en-us/office/vba/library-reference/concepts/getting-started-with-vba-in-office)</sup>

该语言本身的文档涵盖字符集、常量、数据类型、指令、事件、函数、关键字、对象、运算符和语句。<sup>[12](https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/visual-basic-language-reference)</sup> 内部数据类型包括 Boolean（2 字节）、Byte（1 字节，0 到 255）、Currency（8 字节，缩放整数）、Date（8 字节，涵盖 100 年 1 月 1 日至 9999 年 12 月 31 日）和 Decimal（14 字节），以及在模块级别用 Type 语句声明的用户定义类型。<sup>[13](https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/data-type-summary)</sup><sup> • </sup><sup>[14](https://learn.microsoft.com/en-us/office/vba/Language/Reference/user-interface-help/type-statement)</sup>

## 历史与授权

微软于 1993 年在 [Microsoft Excel](https://www.edgepedia.org/zh/microsoft-excel) 中首次实现了后来成为 VBA 的通用高级应用程序可编程能力。<sup>[4](https://news.microsoft.com/source/1996/06/04/microsoft-announces-widespread-licensing-and-availability-of-visual-basic-applications-edition/?ver=3.7.1)</sup> 1996 年 6 月，该公司宣布广泛授权 Visual Basic for Applications 5.0，其重新设计的开发环境与独立版 Visual Basic 共享特性，包括对 ActiveX 控件的支持和新的跨平台窗体包。该引擎计划出现在下一版本的 Microsoft Office 和 Visual Basic 中，以及 Adobe、Autodesk、Micrografx、NetManage、Rockwell Software、Sagent、SAP 和 Visio 等公司产品的未来版本中。<sup>[4](https://news.microsoft.com/source/1996/06/04/microsoft-announces-widespread-licensing-and-availability-of-visual-basic-applications-edition/?ver=3.7.1)</sup> VBA 是可扩展 Visual Basic 家族的第二个成员，拥有完整的 Visual Basic 语言语法；集成在 [Internet Explorer](https://www.edgepedia.org/zh/internet-explorer) 3.0 中的 Visual Basic Scripting Edition 提供了一个兼容子集，针对互联网可编程性进行了优化。<sup>[4](https://news.microsoft.com/source/1996/06/04/microsoft-announces-widespread-licensing-and-availability-of-visual-basic-applications-edition/?ver=3.7.1)</sup>

1999 年 4 月 28 日，微软通过 VBA 授权计划向第三方软件厂商发布了 VBA 6.0 版，称其为 Microsoft Office 中的开发环境，属于当时全球超过 320 万开发者使用的语言家族。<sup>[2](https://news.microsoft.com/source/1999/04/28/microsoft-releases-visual-basic-for-applications-version-6-0-to-software-vendors/)</sup>

## 64 位 VBA 与版本 7

VBA 6 及更早版本隐式面向 32 位平台，没有专门用于指针或句柄的数据类型，因此代码使用 Long 类型（32 位 4 字节整数）来引用它们。在 64 位环境中，指针和句柄是 8 字节的量，无法容纳在 32 位类型中；将 64 位加载到 32 位数据类型会截断值，可能导致内存越界、意外结果和应用程序失败。<sup>[5](https://learn.microsoft.com/en-us/office/vba/language/concepts/getting-started/64-bit-visual-basic-for-applications-overview)</sup>

Office 2010 引入了 VBA 7，针对此问题提供了三项语言特性：<sup>[5](https://learn.microsoft.com/en-us/office/vba/language/concepts/getting-started/64-bit-visual-basic-for-applications-overview)</sup>

- **LongPtr**，一个类型别名，在 32 位 Office 中解析为 Long，在 64 位 Office 中解析为 LongLong，推荐用于指针和句柄。
- **LongLong**，仅在 64 位 Office 中可用的有符号 64 位整数，可容纳从 -9,223,372,036,854,775,808 到 9,223,372,036,854,775,807 的值；不允许隐式转换为更小的整数类型。<sup>[5](https://learn.microsoft.com/en-us/office/vba/language/concepts/getting-started/64-bit-visual-basic-for-applications-overview)</sup>
- **PtrSafe**，一个关键字，断言 Declare 语句（用于调用 [Windows API](https://www.edgepedia.org/zh/windows-api) 函数的构造）对 64 位 Office 是安全的。在 64 位 Office 中运行时，所有 Declare 语句都必须包含 PtrSafe，任何持有 64 位量的参数或返回值也必须改为 64 位类型；仅添加 PtrSafe 而返回值仍为 Long 仍会截断值。<sup>[5](https://learn.microsoft.com/en-us/office/vba/language/concepts/getting-started/64-bit-visual-basic-for-applications-overview)</sup>

必须在新旧版本上运行的代码使用条件编译常量 VBA7 和 Win64 来选择适当的 Declare 形式。<sup>[5](https://learn.microsoft.com/en-us/office/vba/language/concepts/getting-started/64-bit-visual-basic-for-applications-overview)</sup> 默认情况下，Office 2010、2013 和 2016 安装的是 32 位版本；从 Office 2019 和 [Microsoft 365](https://www.edgepedia.org/zh/microsoft-365) 开始，默认为 64 位版本。<sup>[5](https://learn.microsoft.com/en-us/office/vba/language/concepts/getting-started/64-bit-visual-basic-for-applications-overview)</sup>

## 宏安全

由于宏病毒可以附着在文档上并在文档打开时执行，VBA 一直是反复出现的恶意软件载体。Melissa 病毒是一个包含恶意 VBA 宏的 [Microsoft Word](https://www.edgepedia.org/zh/microsoft-word) 文档，它通过将宿主文档作为电子邮件附件发送给每个受害者通讯簿中的联系人，在互联网上传播。<sup>[15](https://tsapps.nist.gov/publication/get_pdf.cfm?pub_id=152039)</sup> 安全机构因此建议禁用通过电子邮件传输的 Microsoft Office 文件中的宏脚本。<sup>[16](https://www.cisa.gov/stopransomware/ransomware-guide)</sup>

微软分阶段做出了回应。较旧的 Office 版本使用带有高、中、低级别的安全对话框：在高设置下，只有来自受信任源的经过数字签名的 VBA 项目可以运行，未签名的项目被禁用且无警告；在中设置下，用户选择是否启用每个项目；在低设置下，所有病毒防护均被关闭。<sup>[17](https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/security-dialog-box)</sup> 当前的默认设置更为严格：来自互联网的文件中的宏被阻止，用户打开文件时会看到“安全风险”横幅。<sup>[8](https://learn.microsoft.com/en-us/microsoft-365-apps/security/internet-macros-blocked)</sup>

阻止决策遵循一个确定的顺序。Windows 会为来自不受信任位置（如浏览器下载和电子邮件附件）的文件添加 Web 标记（MOTW），在 NTFS 卷上的备用数据流中记录为 ZoneId 值；ZoneId 为 3（互联网）意味着宏默认被阻止，而 2（受信任站点）则意味着不被阻止。<sup>[8](https://learn.microsoft.com/en-us/microsoft-365-apps/security/internet-macros-blocked)</sup> 来自受信任位置的文件打开时启用宏，宏带有受信任发布者数字签名的文件也是如此；策略随后可以允许或阻止宏，只有上述检查均未通过的文件才会触发默认阻止。<sup>[8](https://learn.microsoft.com/en-us/microsoft-365-apps/security/internet-macros-blocked)</sup> 用户和管理员可以通过“属性”中的“解除锁定”复选框或 PowerShell 的 Unblock-File 命令 解除单个文件的阻止，将网络位置指定为受信任站点，或将模板保存到受信任位置，但微软警告说应谨慎管理这些位置，因为其中的宏不会被阻止。<sup>[8](https://learn.microsoft.com/en-us/microsoft-365-apps/security/internet-macros-blocked)</sup>

## VBA 与更新的 Office 自动化并存

微软将 VBA 定位为以桌面为中心：它可以与用户桌面交互，并连接 COM 和 OLE 等技术，但没有便捷的方式访问互联网。Office Scripts 是其基于 JavaScript 的 Excel 对应产品，在 Web 版 Excel、Windows 和 Mac 上的通用运行时中运行，可以调用有限的 Web 服务，并可由 Power Automate 流触发；VBA 没有 Power Automate 连接器，因此所有受支持的 VBA 场景都需要用户在场参与宏的执行。<sup>[9](https://learn.microsoft.com/en-us/office/dev/scripts/resources/vba-differences)</sup> VBA 宏拥有与 Excel 相同的安全许可，可完全访问桌面，而 Office Scripts 仅访问工作簿且不接收身份验证令牌。<sup>[9](https://learn.microsoft.com/en-us/office/dev/scripts/resources/vba-differences)</sup> VBA 目前对 Excel 功能的覆盖更完整，尤其是在桌面客户端上。<sup>[9](https://learn.microsoft.com/en-us/office/dev/scripts/resources/vba-differences)</sup>

在微软之外，[LibreOffice](https://www.edgepedia.org/zh/libreoffice) Basic 实现了一种宏语言，在 VBA 兼容模式下旨在与 VBA 互操作，以便文档中嵌入的宏可以运行。<sup>[18](https://docs.libreoffice.org/basic.html)</sup> LibreOffice 可以加载 Microsoft Office 文件中包含的宏，在其 Basic IDE 中查看和编辑；除少数例外，这两个套件无法运行相同的宏代码，因为即使语言相似，对象和方法也不同。<sup>[19](https://help.libreoffice.org/latest/en-US/text/shared/guide/ms_user.html)</sup>

## References

1. [Support for VBA Macros, LibreOffice Help](https://help.libreoffice.org/latest/en-US/text/sbasic/shared/vbasupport.html?DbPAR=WRITER&System=UNIX)
2. [Microsoft Releases Visual Basic for Applications Version 6.0 To Software Vendors, Microsoft News (April 28, 1999)](https://news.microsoft.com/source/1999/04/28/microsoft-releases-visual-basic-for-applications-version-6-0-to-software-vendors/)
3. [Getting started with VBA in Office, Microsoft Learn](https://learn.microsoft.com/en-us/office/vba/library-reference/concepts/getting-started-with-vba-in-office)
4. [Microsoft Announces Widespread Licensing and Availability Of Visual Basic, Applications Edition, Microsoft News (June 4, 1996)](https://news.microsoft.com/source/1996/06/04/microsoft-announces-widespread-licensing-and-availability-of-visual-basic-applications-edition/?ver=3.7.1)
5. [64-bit Visual Basic for Applications overview, Microsoft Learn](https://learn.microsoft.com/en-us/office/vba/language/concepts/getting-started/64-bit-visual-basic-for-applications-overview)
6. [Security notes for Office solution developers, Microsoft Learn](https://learn.microsoft.com/cs-cz/office/vba/library-reference/concepts/security-notes-for-microsoft-office-solution-developers)
7. [File format reference for Word, Excel, and PowerPoint, Microsoft Learn](https://learn.microsoft.com/en-us/office/compatibility/office-file-format-reference)
8. [Macros from the internet are blocked by default in Office, Microsoft Learn](https://learn.microsoft.com/en-us/microsoft-365-apps/security/internet-macros-blocked)
9. [Differences between Office Scripts and VBA macros, Microsoft Learn](https://learn.microsoft.com/en-us/office/dev/scripts/resources/vba-differences)
10. [MS-OVBA: Office VBA File Format Structure, Microsoft Learn](https://learn.microsoft.com/en-us/openspecs/office_file_formats/ms-ovba/575462ba-bf67-4190-9fac-c275523c75fc)
11. [Understanding objects, methods, properties, and events (VBA), Microsoft Learn](https://learn.microsoft.com/en-us/office/vba/language/concepts/getting-started/understanding-objects-properties-methods-and-events)
12. [Visual Basic language reference, Microsoft Learn](https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/visual-basic-language-reference)
13. [Data type summary, Microsoft Learn](https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/data-type-summary)
14. [Type statement (VBA), Microsoft Learn](https://learn.microsoft.com/en-us/office/vba/Language/Reference/user-interface-help/type-statement)
15. [Guidelines on Active Content and Mobile Code, NIST](https://tsapps.nist.gov/publication/get_pdf.cfm?pub_id=152039)
16. [#StopRansomware Guide, CISA](https://www.cisa.gov/stopransomware/ransomware-guide)
17. [Security dialog box, Microsoft Learn](https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/security-dialog-box)
18. [basic (StarBASIC Interpreter), LibreOffice documentation](https://docs.libreoffice.org/basic.html)
19. [Using Microsoft Office and LibreOffice, LibreOffice Help](https://help.libreoffice.org/latest/en-US/text/shared/guide/ms_user.html)

---
*Initially written Sep 17, 2026 · Reviewed: Sep 30, 2026; Oct 15, 2026 · Edited: Sep 30, 2026; Oct 15, 2026 · Last review: Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Programming languages › Individual programming languages › C-family and imperative languages

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/visual-basic-for-applications

English original: https://www.edgepedia.org/visual-basic-for-applications
