Walkthrough: Embedding Type Information from Microsoft Office Assemblies in Visual Studio (C#)

If you embed type information in an application that references COM objects, you can eliminate the need for a primary interop assembly (PIA). Additionally, the embedded type information enables you to achieve version independence for your application. That is, your program can be written to use types from multiple versions of a COM library without requiring a specific PIA for each version. This is a common scenario for applications that use objects from Microsoft Office libraries. Embedding type information enables the same build of a program to work with different versions of Microsoft Office on different computers without the need to redeploy either the program or the PIA for each version of Microsoft Office.


Your computer might show different names or locations for some of the Visual Studio user interface elements in the following instructions. The Visual Studio edition that you have and the settings that you use determine these elements. For more information, see Personalizing the IDE.


This walkthrough requires the following:

  • A computer on which Visual Studio and Microsoft Excel are installed.

  • A second computer on which the .NET Framework 4 or higher and a different version of Excel are installed.

To create an application that works with multiple versions of Microsoft Office

  1. Start Visual Studio on a computer on which Excel is installed.

  2. On the File menu, choose New, Project.

  3. In the New Project dialog box, in the Project Types pane, make sure that Windows is selected. Select Console Application in the Templates pane. In the Name box, enter CreateExcelWorkbook, and then choose the OK button. The new project is created.

  4. In Solution Explorer, open the shortcut menu for the References folder and then choose Add Reference.

  5. On the .NET tab, choose the most recent version of Microsoft.Office.Interop.Excel. For example, Microsoft.Office.Interop.Excel Choose the OK button.

  6. In the list of references for the CreateExcelWorkbook project, select the reference for Microsoft.Office.Interop.Excel that you added in the previous step. In the Properties window, make sure that the Embed Interop Types property is set to True.


    The application created in this walkthrough runs with different versions of Microsoft Office because of the embedded interop type information. If the Embed Interop Types property is set to False, you must include a PIA for each version of Microsoft Office that the application will run with.

  7. Open the Program.cs file. Replace the code in the file with the following code:

    using System;  
    using System.Collections.Generic;  
    using System.Linq;  
    using System.Text;  
    using System.IO;  
    using Excel = Microsoft.Office.Interop.Excel;  
    namespace CreateExcelWorkbook  
        class Program  
            static void Main(string[] args)  
                int[] values = {4, 6, 18, 2, 1, 76, 0, 3, 11};  
                CreateWorkbook(values, @"C:\SampleFolder\SampleWorkbook.xls");  
            static void CreateWorkbook(int[] values, string filePath)  
                Excel.Application excelApp = null;  
                Excel.Workbook wkbk;  
                Excel.Worksheet sheet;  
                        // Start Excel and create a workbook and worksheet.  
                        excelApp = new Excel.Application();  
                        wkbk = excelApp.Workbooks.Add();  
                        sheet = wkbk.Sheets.Add() as Excel.Worksheet;  
                        sheet.Name = "Sample Worksheet";  
                        // Write a column of values.  
                        // In the For loop, both the row index and array index start at 1.  
                        // Therefore the value of 4 at array index 0 is not included.  
                        for (int i = 1; i < values.Length; i++)  
                            sheet.Cells[i, 1] = values[i];  
                        // Suppress any alerts and save the file. Create the directory   
                        // if it does not exist. Overwrite the file if it exists.  
                        excelApp.DisplayAlerts = false;  
                        string folderPath = Path.GetDirectoryName(filePath);  
                        if (!Directory.Exists(folderPath))  
                    sheet = null;  
                    wkbk = null;  
                    // Close Excel.  
                    excelApp = null;  
  8. Save the project.

  9. Press CTRL+F5 to build and run the project. Verify that an Excel workbook has been created at the location specified in the example code: C:\SampleFolder\SampleWorkbook.xls.

To publish the application to a computer on which a different version of Microsoft Office is installed

  1. Open the project created by this walkthrough in Visual Studio.

  2. On the Build menu, choose Publish CreateExcelWorkbook. Follow the steps of the Publish Wizard to create an installable version of the application. For more information, see Publish Wizard (Office Development in Visual Studio).

  3. Install the application on a computer on which the .NET Framework 4 or higher and a different version of Excel are installed.

  4. When the installation is finished, run the installed program.

  5. Verify that an Excel workbook has been created at the location specified in the sample code: C:\SampleFolder\SampleWorkbook.xls.

See Also

Walkthrough: Embedding Types from Managed Assemblies in Visual Studio (C#)
/link (C# Compiler Options)