Section 5.12.Create Excel .NET Applications


5.12. Create Excel .NET Applications

A third and final way for Excel and .NET to interact is through the Visual Studio .NET Tools for Office. This set of tools includes Visual Studio .NET project templates for Excel and Word. These project templates allow you to link a specific document to a .NET assembly that loads whenever the user opens that document. The .NET code in the assembly can control Excel and respond to Excel events as described in the preceding sections.

5.12.1. How to do it

To create an Excel application in Visual Studio .NET:

  1. From the Project menu, choose New, Project. Visual Studio .NET displays the New Project dialog box.

  2. Select the Microsoft Office System Projects, Visual Basic Projects project type and Excel Workbook template, give the project a descriptive name and click OK. Visual Studio .NET starts the Microsoft Office Project Wizard to walk you through.

  3. Click Finish to create the project folder and empty workbook and code template files.

  4. Visual Studio .NET doesn't automatically add the workbook to the project, so it is a good idea to add it at this point. From the Project menu, choose Add Existing Item and then select the .xls file found in the application folder.

  5. Once the workbook is added to the project, select the workbook in the Solution Explorer and set its Build Action property to Content. This will ensure that the workbook is distributed with your application if you create an installation program.

5.12.2. How it works

When Visual Studio .NET creates an Excel project, it adds references to the Microsoft Office and Excel 2003 PIAs, adds Imports statements to provide short cuts to the Office and Excel classes, and generates code to ThisApplication and ThisWorkbook objects as shown in Figure 5-15.

Visual Studio .NET links the workbook to the project's assembly through two custom document properties: _AssemblyLocation0 and _AssemblyName0. The _AssemblyLocation0 property corresponds to the Visual Studio .NET project's Assembly Link Location property as shown in Figure 5-16.

Figure 5-15. A newly created Excel project in Visual Studio .NET


Figure 5-16. Setting Assembly Link Location changes the _AssemblyLocation0 custom document property in the Excel workbook


You might notice that an Excel project has both a \bin and a \ projectname _bin folder. Excel projects write assembly output first to the \bin folder, then copy that file to the secondary folder. This allows the project to compile even if the Excel workbook has the assembly open in the secondary folder, plus it allows the project to be automatically deployed to a public location every time you build ita process Microsoft calls no touch deployment .

When you open an Excel workbook that has _AssemblyLocation0 and _AssemblyName0 custom properties, Excel automatically starts the Office Toolkit Loader addin (Otkloadr.dll). The Office Toolkit Loader addin then starts the .NET assembly specified in the AssemblyLocation0 and _AssemblyName0 properties.


Tip: Visual Studio .NET Tools for Office are built for use with the .NET Framework, Version 1.1. Applications built with these tools work with later versions of the .NET Framework, but the security configuration and debugging portions of the tools do not. If you have a later version of .NET installed, you must set the .NET security policy for the project manually and attach the debugger to the process manually.

5.12.3. What about...

To learn how toLook here
Create Excel .NET ApplicationsSearch msdn.microsoft.com for "Creating Office Solutions"




    Excel 2003 Programming. A Developer's Notebook
    Excel 2003 Programming: A Developers Notebook (Developers Notebook)
    ISBN: 0596007671
    EAN: 2147483647
    Year: 2004
    Pages: 133
    Authors: Jeff Webb

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net