Interoperability between VBA and Visual Studio Tools for the Office System (3.0)

Summary: This Visual How-to article shows you how to activate interoperability between existing Microsoft Visual Basic for Applications (VBA) applications and Microsoft Visual Studio Tools for the Microsoft Office system (3.0).

Office Visual How To

Applies to: 2007 Microsoft Office System, Microsoft Office Outlook 2007, Microsoft Visual Studio Tools for the Microsoft Office System (3.0)

Steven Hansen, OfficeZealot.com

October 2007

Overview

Microsoft Visual Studio Tools for the Microsoft Office system (3.0), part of Microsoft Visual Studio 2008, has a new feature called VSTO/VBA interoperability that enables you to discover and call Visual Studio Tools for Office–based code from within Microsoft Visual Basic for Applications (VBA) macros. This feature also supports Microsoft IntelliSense within the Visual Basic Editor so that you can discover and call your Visual Studio Tools for Office–based code by using IntelliSense.

The VSTO/VBA interoperability feature enables you to add new Visual Studio Tools for Office functionality to existing VBA applications, preserving your investment in VBA. This eliminates the need to rewrite the existing application using Visual Studio Tools for the Office system (3.0).

This Visual How-to article demonstrates interoperability between Visual Studio Tools for the Office system (3.0) and VBA in a Microsoft Office Excel 2007 workbook project written using Microsoft Visual Basic .NET.

Code It

You can activate VSTO/VBA interoperability in a Visual Basic project without writing a line of code in Visual Studio. Activating interoperability in a Microsoft Visual C# project is more complex and is outside the scope of this article.

Introducing VBA to Visual Studio Tools for the Office System (3.0)

Before activating the VSTO/VBA feature, you need to base your Visual Studio Tools for the Office system (3.0) project on an existing document that contains VBA macros. This section shows you how to create a simple example using Excel 2007. The six major steps to create the example are as follows:

  1. Add VBA to a new workbook, or start with an existing workbook that contains VBA macros.

  2. Create a Visual Basic Excel Workbook project using Visual Studio Tools for the Office system (3.0), and copy the workbook from step 1 as the template for the project.

  3. Activate COM callers on one of the host object classes in the project.

  4. Implement the desired functionality in the host object class from step 3.

  5. Build the project.

  6. Open the extended workbook and modify the VBA code to call your Visual Studio Tools for the Office system (3.0) functionality.

The following procedure demonstrates how to activate VSTO/VBA interoperability.

To activate VSTO/VBA interoperability

  1. Open a new workbook in Microsoft Office Excel 2007.

  2. Switch to the Visual Basic Editor by pressing ALT+F11.

  3. Double-click Sheet1 in the Project Explorer window (press CTRL+R if it is not visible) and enter the HelloWorldFromVBA procedure.

  4. Test the code by placing the cursor in the HelloWorldFromVBA procedure and pressing F5.

  5. Save the workbook as an Excel macro-enabled workbook.

  6. Create a Visual Basic Excel 2007 Workbook project in Visual Studio.

  7. When you select a document for the application, select Copy an existing document and select the workbook from step 5 into which you added the HelloWorldFromVBA procedure.

  8. After you create the project, right-click Sheet1 and select View Designer.

  9. Locate ActivateComCallers in the Properties window and set the value to True.

  10. Right-click Sheet1.vb in Solution Explorer and select View Code.

  11. Add a Visual Studio Tools for the Office system (3.0) implementation named HelloWorldFromVSTO.

  12. Build and run the project. After the customization is running, open the Visual Basic Editor and view the code associated with Sheet1. Notice that Visual Studio added a VBA procedure to Sheet1.

  13. Modify the existing HelloWorldFromVBA procedure as shown in the following listing.

  14. Place the cursor inside the HelloWorldFromVSTO procedure and press F5. Notice that instead of seeing a VBA message box, you see the message box from your Visual Studio Tools for the Office system (3.0) project.

Read It

To activate interoperability between Visual Studio Tools for the Office system (3.0) and a Visual Basic project, you modify the EnableComCallers property in a properties window. Although you only need to modify one property, you should remember some important points as you experiment with this feature.

Open XML File Formats and the VBA Interoperability Feature

One of the major changes in the 2007 Microsoft Office system is the new file formats associated with Microsoft Office documents. The default Open XML file format for Microsoft Office documents does not support embedded macros such as VBA. To save a document that contains VBA, you must choose the macro-enabled format, or a legacy file format such as a Microsoft Office Excel 2003 workbook. Because the VSTO/VBA interoperability feature is all about integrating VBA, you must choose to use documents that support VBA to use this feature.

Documents Must Include Embedded VBA

To activate VSTO/VBA interoperability in a Visual Studio Tools for the Office system (3.0) project, you must start with a document that contains VBA code. If you try to use the feature in a document that does not contain macros, Visual Studio displays the message The host item cannot be exposed to VBA code because the VBA project in the document cannot be loaded.

Exposing Visual Studio Tools for the Office system (3.0) Procedures to VBA

When you activate the EnableComCallers property on a host item class within a Visual Studio Tools for the Office system (3.0) project, all of the public properties and methods that are implemented within the class are visible to VBA. To call your Visual Studio Tools for the Office system (3.0) procedures from within VBA, use the CallVSTOAssembly property that is associated with the matching host item object in VBA.

For example, if you exposed the Sheet1 class in Visual Studio Tools for the Office system (3.0), use the CallVSTOAssembly property associated with the Sheet1 object in Excel 2007.

Manage Projects Carefully

It is tempting to make changes to the VBA code in the document that is located in your Debug directory (the document that opens when you debug your Visual Studio Tools for the Office system (3.0) project). Remember that this document is overwritten every time you build or execute your project in debug mode. If you make changes to the VBA code and save the document, unless you save it to a different location, your changes are overwritten the next time you build your Visual Studio Tools for the Office system (3.0) project. Consequently, you need to manage your project carefully to guard against this.

See It Splash screen of video

Watch the Video

Video Length: 00:12:50

File Size: 26.7 MB WMV

Explore It