Home > Articles > Programming > Visual Studio

This chapter is from the book

VSTO and Managed Code

When you create VBA solutions for Word and Excel, your code typically is stored in the document or in an attached or global template. To access code in a template separate from your solution, you set a reference from your document to the template.

However, when you create VSTO solutions, your code is stored in an assembly. You can set references in your project to other assemblies, such as .NET Framework assemblies and interop assemblies, but you cannot set a reference to other VSTO solution assemblies. Only one VSTO solution assembly can be associated with a document or workbook. However, multiple documents can reference the same solution assembly. This is the case when you create multiple documents based on a VSTO-customized template. Note that because Outlook solutions are application-level, you can load multiple add-ins into Outlook. The same is true for add-ins created with VSTO 2005 SE.

Primary Interop Assemblies

Office applications, such as Word and Excel, are written in unmanaged code. For your VSTO solution (managed code) to interoperate with the unmanaged COM objects in the Office application, it must use an interoperability assembly. Visual Studio can create an interoperability assembly for you when you set a reference to a COM type library, but generating an interoperability assembly in this way is not recommended. Instead, you should use the official interoperability assembly that is provided by the publisher of the type library. This is known as the primary interop assembly (PIA). If the PIAs are installed on your computer and you set a reference to the type library, Visual Studio automatically loads the PIA instead of generating a new interop assembly.

Microsoft provides PIAs for its Office applications. The name of the Excel PIA is Microsoft.Office.Interop.Excel.dll. Word and Outlook follow the same naming convention for their PIAs: Microsoft.Office.Interop.Word.dll and Microsoft.Office.Interop.Outlook.dll.

The PIAs get installed in your computer's GAC when you run a complete installation of Office 2003. Whenever you create a new VSTO solution, Visual Studio automatically adds to your solution a reference to the appropriate Office PIA and any of its dependent assemblies. You can view the contents of the GAC by opening the Windows\assembly directory of your root drive. Figure 3.7 shows the Microsoft PIAs.

Figure 3.7

Figure 3.7 Microsoft Office PIAs displayed in the GAC

The namespace for these PIAs is determined by the name of the assembly. VSTO automatically creates an alias for these namespaces, as shown in Table 3.3.

Table 3.3. Excel, Word, and Outlook PIAs, Namespaces, and Aliases













If the PIAs have not been correctly installed on your development machine because a complete installation of Office 2003 was not performed, you can run a reinstall or repair of Office 2003, assuming that .NET Framework 1.1 or later is already installed on your computer. Alternatively, you can use Add or Remove Features in the Maintenance Mode Options section of the Microsoft Office 2003 Setup, and click Next (see Figure 3.8). For information on redistributing the PIAs when you deploy your solutions, see Chapter 11.

Figure 3.8

Figure 3.8 Maintenance Mode Options in Microsoft Office 2003 Setup

On the next page of the Setup wizard, select Choose Advanced Customization of Applications, and then click Next.

Set each of the following components to Run from My Computer. See Figure 3.9 for an example.

Figure 3.9

Figure 3.9 Advanced Customization in Microsoft Office 2003 Setup

  • Microsoft Office Excel | .NET Programmability Support
  • Microsoft Office Outlook | .NET Programmability Support
  • Microsoft Office Word | .NET Programmability Support
  • Office Tools | Microsoft Forms 2.0 .NET Programmability Support
  • Office Tools | Smart Tag .NET Programmability Support
  • Office Tools | Microsoft Graph, .NET Programmability Support

Solution Assemblies

As you learned earlier in this chapter, an assembly is a collection of classes and functionality that is stored as an executable file (.exe) or a library (.dll). When you build a VSTO solution, the code is compiled and stored in a single assembly (DLL file) located in the \bin\debug (or \bin\release) directory of your solution. The assembly isn't stored inside the document, but the document does include an application manifest, which contains information about the name and location of the assembly. There are two manifests: an application manifest (which is stored in the document via an embedded control called the Runtime Storage Control) and a deployment manifest, which is located in the same directory where the assembly is deployed.

Even though you can associate a VSTO solution assembly only with a particular document (you can have only one solution assembly associated with a document), your solution assembly can reference other assemblies. For example, to add a common group of user controls to the actions pane, you can save a library of user controls in an assembly and then reference that assembly from multiple VSTO solutions.

How do you determine which VSTO assembly is associated with a particular document? You look at the document properties. You can view the document properties of a Word document or Excel workbook by clicking Properties in the File menu. A VSTO-enabled document has two custom document properties that indicate that the document is a VSTO solution. The first custom property is named _AssemblyName. If the value of _AssemblyName is an asterisk (*), it means that the document has an associated VSTO customization (assembly). The second custom property, _AssemblyLocation, stores the globally unique identifier (GUID) of the Runtime Storage Control (which contains the application manifest and information about where to locate the associated assembly). Figure 3.10 shows the custom document properties of a Word solution.

Figure 3.10

Figure 3.10 Custom document properties of a Word solution

Adding Customization to Existing Documents

You can attach an existing solution assembly to an uncustomized document or worksheet in one of two ways. The first is to add the _AssemblyName property with a value of asterisk (*), add the _AssemblyLocation custom property with the location of the deployment manifest as its value, and then save, close, and reopen the document. The second approach involves using the AddCustomization method of the ServerDocument class. These techniques are described in more detail in Chapter 13, Advanced Topics in VSTO 2005.

Running VSTO Solutions

For a VSTO solution to run, the assemblies must have full trust permissions. On the development machine, full trust is automatically granted to your solution's assembly and any referenced assemblies whenever you build the project. The evidence used for this trust is based on location (the URL of the assembly). When you deploy your solution, you need to grant full trust to the document and the assembly by using a strong name or by digitally signing the assembly. This procedure is discussed in greater detail in Chapter 11.

As a developer, you can run your customization by pressing F5 or by clicking Start Debugging from the Debug menu in Visual Studio. You can also open the document or workbook that is stored in the /debug/bin directory of the location in which you saved your solution, or press Ctrl+F5 to run the solution without debugging it.

An end user can run a solution in one of two ways.

  1. To run a document-level customization, the user opens the document or workbook that has an associated customization assembly. Alternatively, the user can create a new document or workbook that is based on a template that has an associated customization assembly.
  2. To run an application-level customization (add-in), users have two options. They can open the application, which contains instructions to load an add-in when the application starts. Or they can manually enable the add-in in the COM Add-ins dialog box of the application.

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information

To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.


Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.


If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information

Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.


This site is not directed to children under the age of 13.


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information

If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information

Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents

California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure

Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact

Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice

We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020