Creating Office customizations with VBA has some similarities to creating Office solutions with VSTO, but there are many differences.
When you create a VBA solution, you start in the Office application. In Word and Excel, you can press ALT+F11 to open the Visual Basic Editor. Within the VBE, you have access to projects for any documents that are currently open, templates that the document is based on, and any globally accessible templates. You can choose to add your code in a module within the document, or in an attached or globally available template, such as Normal.dot.
When you add your code to ThisDocument or ThisWorkbook, or to a new module that you've added to the document project, that code will be available only to the document or workbook in which you've added the code. If you add the code to a template, the code will be available either to documents based on that template or, in the case of a global template, to any document that is opened using the application.
You can access the VBE from Outlook, but Outlook can support only one VBA project at a time. Typically, developers create Component Object Model (COM) add-ins for Outlook using the IDTExtensibility2 interface.
When you create a VSTO solution, you start in the Visual Studio IDE. Here you can choose to create a document or template solution for Word or Excel, or an add-in solution for Outlook. If you create an Outlook add-in, it is available to the entire Outlook application. If you choose a Word or Excel document, the code is available only to that document. If you choose a Word or Excel template, the code is available to any document that is created from the template.
When you use VSTO to create a Word or Excel customization, you have full access to the Word or Excel application and object model from within Visual Studio. When you create an add-in, you have access to all the objects in the application through the Application object, but you don't have direct access to the application from within Visual Studio.
The code that you write for a VSTO document-level solution is stored in a code file associated with the document. This is often referred to as code-behind file because the code supports the document functionality behind the scenes in the form of an assembly, rather than in embedded code as VBA does. When the code file is compiled into an assembly (DLL), it is then associated with the document. This compiled code can be accessed only at the document level (and not at the application level).
You can also create a VSTO solution based on an Excel template or a Word template. In that case, whenever a document is opened based on that template, your VSTO customization code will run. You cannot create application-level customizations for Word 2003 and Excel 2003 using VSTO; however, you can create application-level add-ins for several Office applications (including Word 2003 and Excel 2003) using VSTO 2005 SE. You'll learn more about this in Chapter 14.
The application-level solutions of VSTO and VSTO 2005 SE are created in the form of an add-in that gets loaded whenever the application is started and is unloaded whenever the application is closed. A user can also manually load and unload the add-in. You can create add-ins that customize your application's user interface, such as adding new menus or menu items in Excel 2003, adding buttons to a Ribbon in Word 2007, or performing a particular action when an application event occurs.
Using VBA, you create application-level customizations in Word by adding code to a global template. This might be the Normal.dot template or a custom template that you make global to the application by storing the template in the Startup directory. This scenario is not supported in VSTO because VSTO creates customizations that are specific only to an individual document rather than every document that is opened in Word. If you create a VSTO customization using a Word template and then store it in the Startup directory, the customization code associated with the template is not available to all open documents; it runs only when a document based on the template is open. If you want to create an application-level customization for Word, you should create an add-in project using VSTO 2005 SE.
When you use VBA to customize Excel, you can create a type of application-level add-in that uses an Excel add-in file (.xla file). However, you cannot create a VSTO solution using an existing .xla file because this file type is not supported in project creation. Although you can create an Excel VSTO solution and then later save the file as an .xla file that runs the customization code, this scenario is not supported by Microsoft.
When you open Visual Studio 2005 and create a new project, you will find a number of Office-related project templates available. The way in which these projects appear in the New Project dialog box differs depending on the environment settings you selected when you installed Visual Studio. In this book, we assume that you are using the Visual Basic profile. In Chapter 2 you'll learn more about the Visual Studio environment settings and ways you can change them.
With a default installation of VSTO, you can create a Word document, a Word template, an Excel workbook, an Excel template, or an Outlook add-in project. You can optionally install the InfoPath toolkit, making the InfoPath template available, as shown in Figure 1.3. To view these project templates, point to New on the File menu and then click Project. You can expand the Visual Basic node to reveal the project template categories. To display the VSTO project templates in the Templates pane of the New Project dialog box, click the Office node.
VSTO templates in the New Project dialog box
VSTO templates in the New Project dialog box
If you've installed VSTO 2005 SE on top of VSTO, additional templates are available in the New Project dialog box, as shown in Figure 1.4.
Notice that there are a number of add-in project templates for Office 2007 in the 2007 Add-ins node, as well as project templates for Office 2003 in the 2003 Add-ins node.
The Excel Workbook project template enables you to create a VSTO customization for an Excel workbook (.xls file). When you choose Excel Workbook in the New Project dialog box and click OK, Visual Studio 2005 presents you with the Visual Studio Tools for Office Project Wizard. Using this wizard, you can choose to customize a new workbook, or you can select an existing workbook, as shown in Figure 1.5.
To select an existing document, click Browse and navigate to the workbook that you want to create. Rather than use the selected workbook, VSTO makes a copy and saves it in the same directory where your VSTO solution is saved.
The Excel Template project template enables you to create a VSTO customization for an Excel template (.xlt file). When you select Excel Template, you can either create a new template or select an existing one. To select an existing template, you must select a file that has an .xlt extension. If you want to create a template based on an existing workbook, you save the workbook as a template outside Visual Studio before selecting it in the wizard. VSTO copies the template to the same directory where your VSTO solution is saved.
The Word Document project template lets you create a VSTO customization for a Word document (.doc file). When you select Word Document in the New Project dialog box and click OK, Visual Studio displays the Visual Studio Tools for Office Project Wizard. In this wizard, you either create a new document, or select an existing one. VSTO makes a copy of the selected document and saves it to the same directory where your VSTO solution is saved.
The Word Template project template enables you to create a VSTO customization for a Word template (.dot file). When you select Word Template, you can create a new template or select an existing one. To select an existing template, you select a file that has a .dot extension. To create a template based on an existing document, you must first save the document as a template outside Visual Studio before selecting it in the wizard. VSTO copies the template to the same directory where your VSTO solution is saved.
In the Outlook Add-in project template, you can create an add-in for Outlook using VSTO. When you select Outlook Add-in, Visual Studio opens the add-in project in code view, displaying the ThisApplication_ Startup and ThisApplication_Shutdown event handlers. VSTO also creates a setup project for your Outlook solution. The setup project is used to register and deploy the Outlook add-in.
If you've installed VSTO 2005 SE, you will find additional project templates for creating add-ins for several Office 2003 and Office 2007 applications, including Word, Excel, Outlook, PowerPoint, Visio, and
InfoPath (2003 only). The architecture of these add-ins differs slightly from that of the VSTO Outlook add-in. When you create a VSTO 2005 SE add-in, Visual Studio opens the add-in project in code view, displaying the ThisAddin_Startup and ThisAddin_Shutdown event handlers.
Was this article helpful?