Visual Basic for Applications explained

Paradigm:Multi-paradigm
Developer:Microsoft
Latest Release Version:7.1 (Office 2021)
Typing:Static/Dynamic Hybrid, Strong/Weak Hybrid
Influenced By:QuickBASIC, Visual Basic
Operating System:Microsoft Windows, macOS
License:Commercial proprietary software

Visual Basic for Applications (VBA) is an implementation of Microsoft's event-driven programming language Visual Basic 6.0 built into most desktop Microsoft Office applications. Although based on pre-.NET Visual Basic, which is no longer supported or updated by Microsoft (except under Microsoft's "It Just Works" support which is for the full lifetime of supported Windows versions, including Windows 10 and Windows 11), the VBA implementation in Office continues to be updated to support new Office features.[1] [2] VBA is used for professional and end-user development due to its perceived ease-of-use, Office's vast installed userbase, and extensive legacy in business.

Visual Basic for Applications enables building user-defined functions (UDFs), automating processes and accessing Windows API and other low-level functionality through dynamic-link libraries (DLLs). It supersedes and expands on the abilities of earlier application-specific macro programming languages such as Word's WordBASIC. It can be used to control many aspects of the host application, including manipulating user interface features, such as menus and toolbars, and working with custom user forms or dialog boxes.

As its name suggests, VBA is closely related to Visual Basic and uses the Visual Basic Runtime Library. However, VBA code normally can only run within a host application, rather than as a standalone program. VBA can, however, control one application from another using OLE Automation. For example, VBA can automatically create a Microsoft Word report from Microsoft Excel data that Excel collects automatically from polled sensors. VBA can use, but not create, ActiveX/COM DLLs, and later versions add support for class modules.

VBA is built into most Microsoft Office applications, including Office for Mac OS X (except version 2008), and other Microsoft applications, including Microsoft MapPoint and Microsoft Visio. VBA is also implemented, at least partially, in applications published by companies other than Microsoft, including ArcGIS, AutoCAD, Collabora Online, CorelDraw, Kingsoft Office, LibreOffice,[3] SolidWorks,[4] WordPerfect, and UNICOM System Architect (which supports VBA 7.1).

Design

Code written in VBA is compiled[5] to Microsoft P-Code (pseudo-code), a proprietary intermediate language, which the host applications (Access, Excel, Word, Outlook, and PowerPoint) store as a separate stream in COM Structured Storage files (e.g., .doc or .xls) independent of the document streams. The intermediate code is then executed[5] by a virtual machine (hosted by the host application). Compatibility ends with Visual Basic version 6; VBA is incompatible with Visual Basic .NET (VB.NET). VBA is proprietary to Microsoft and, apart from the COM interface, is not an open standard.

Automation

Interaction with the host application uses OLE Automation. Typically, the host application provides a type library and application programming interface (API) documentation which document how VBA programs can interact with the application. This documentation can be examined from inside the VBA development environment using its Object Browser.

Visual Basic for Applications programs which are written to use the OLE Automation interface of one application cannot be used to automate a different application, even if that application hosts the Visual Basic runtime, because the OLE Automation interfaces will be different. For example, a VBA program written to automate Microsoft Word cannot be used with a different word processor, even if that word processor hosts VBA.

Conversely, multiple applications can be automated from the one host by creating Application objects within the VBA code. References to the different libraries must be created within the VBA client before any of the methods, objects, etc. become available to use in the application. This is achieved through what is referred to as Early or Late Binding. These application objects create the OLE link to the application when they are first created. Commands to the different applications must be done explicitly through these application objects in order to work correctly.

As an example, VBA code written in Microsoft Access can establish references to the Excel, Word and Outlook libraries; this allows creating an application that – for instance – runs a query in Access, exports the results to Excel and analyzes them, and then formats the output as tables in a Word document or sends them as an Outlook email.

VBA programs can be attached to a menu button, a macro, a keyboard shortcut, or an OLE/COM event, such as the opening of a document in the application. The language provides a user interface in the form of UserForms, which can host ActiveX controls for added functionality.

Inter-process communication automation includes the Dynamic Data Exchange (DDE) and RealTimeData (RTD) which allows calling a Component Object Model (COM) automation server for dynamic or realtime financial or scientific data.[6]

Security concerns

As with any common programming language, VBA macros can be created with malicious intent. Using VBA, most of the security features lie in the hands of the user, not the author. The VBA host application options are accessible to the user. The user who runs any document containing VBA macros can preset the software with user preferences. End-users can protect themselves from attack by disabling macros from running in an application or by granting permission for a document to run VBA code only if they are sure that the source of the document can be trusted.

In February 2022, Microsoft announced its plan to block VBA macros in files downloaded from the Internet by default in a variety of Office apps due to their widespread use to spread malware.[7]

Version history

Development

As of July 1, 2007, Microsoft no longer offers VBA distribution licenses to new customers. Microsoft intended to add .NET-based languages to the current version of VBA ever since the release of the .NET Framework,[8] of which versions 1.0 and 1.1 included a scripting runtime technology named Script for the .NET Framework.[9] Visual Studio .NET 2002 and 2003 SDK contained a separate scripting IDE called Visual Studio for Applications (VSA) that supported VB.NET.[10] [11] [12] One of its significant features was that the interfaces to the technology were available via Active Scripting (VBScript and JScript), allowing even .NET-unaware applications to be scripted via .NET languages. However, VSA was deprecated in version 2.0 of the .NET Framework, leaving no clear upgrade path for applications desiring Active Scripting support (although "scripts" can be created in C#, VBScript, and other .NET languages, which can be compiled and executed at run-time via libraries installed as part of the standard .NET runtime).

Microsoft dropped VBA support for Microsoft Office 2008 for Mac.[13] [14] VBA was restored in Microsoft Office for Mac 2011. Microsoft said that it has no plan to remove VBA from the Windows version of Office.[15] [16]

With Office 2010, Microsoft introduced VBA7, which contains a true pointer data type: LongPtr. This allows referencing 64-bit address space. The 64-bit install of Office 2010 does not support common controls of MSComCtl (TabStrip, Toolbar, StatusBar, ProgressBar, TreeView, ListViews, ImageList, Slider, ImageComboBox) or MSComCt2 (Animation, UpDown, MonthView, DateTimePicker, FlatScrollBar), so legacy 32-bit code ported to 64-bit VBA code that depends on these common controls will not function. This did not affect the 32-bit version Office 2010.[17] Microsoft eventually released a 64-bit version of MSComCtl with the July 27th, 2017 update to Office 2016.[18]

See also

Notes and References

  1. Web site: Compatibility Between the 32-bit and 64-bit Versions of Office 2010. msdn.microsoft.com.
  2. Web site: o365devx . What's new for VBA in Office 2019 . 2022-05-02 . docs.microsoft.com . en-us.
  3. Web site: Support for VBA Macros . The Document Foundation - LibreOffice. 3 January 2023.
  4. Web site: 2016 SolidWorks Help – VBA. help.solidworks.com. 2016-07-25.
  5. Web site: ACC: Visual/Access Basic Is Both a Compiler and an Interpreter . 2012 . Microsoft . dead . https://web.archive.org/web/20121021000129/http://support.microsoft.com/kb/109382 . 2012-10-21.
  6. Web site: How to set up and use the RTD function in Excel. msdn.microsoft.com.
  7. Web site: Microsoft to Block Office VBA Macros by Default . 2022-09-26 . The Verge . en-US.
  8. Web site: Visual Studio for Applications. dead. https://web.archive.org/web/20071217064436/http://blogs.thinktecture.com/cnagel/archive/2005/09/14/414225.aspx. 2007-12-17.
  9. Web site: Introducing Visual Studio for Applications. msdn.microsoft.com.
  10. Web site: Script Happens .NET. msdn.microsoft.com.
  11. Web site: Microsoft Takes Wraps Off VSA Development Technology. dead. https://web.archive.org/web/20071217062427/http://redmondmag.com/news/article.asp?EditorialsID=126. 2007-12-17.
  12. Web site: VSA scripting in .NET. dead. https://web.archive.org/web/20070211180713/http://www.codeproject.com/csharp/VsaScripting.asp. 2007-02-11.
  13. Web site: WWDC: Microsoft updates Universal status of Mac apps. 2006-08-07. Macworld. 2007-05-25.
  14. Web site: What is Microsoft Office and Office 365 – FAQs.
  15. Web site: The Reports of VBA's Demise Have Been Greatly Exaggerated.
  16. Web site: Clarification on VBA Support . dead . https://web.archive.org/web/20080411025937/http://blogs.msdn.com/excel/archive/2008/01/16/clarification-on-vba-support.aspx . 2008-04-11.
  17. Web site: Compatibility Between the 32-bit and 64-bit Versions of Office 2010. msdn.microsoft.com.
  18. Web site: Release notes for Monthly Channel releases in 2017. learn.microsoft.com. 2022-11-13.