How VSTO Integrates with Visual Studio

When you create a Word or Excel solutions, using VSTO, you can interact with the Word or Excel application directly from within Visual Studio. This is because VSTO is integrated with Visual Studio.

Word and Excel as Designers

After you have created a new Excel or Word document or template project, the document is available within the Visual Studio environment as a designer. In fact, the entire application is available to you right inside Visual Studio!

A designer is a container for the user interface of the application you are building. For example, if you create a UserForm in a Word VBA solution, the VBE displays the UserForm as a designer for your application. You can then add controls to the UserForm. This same experience is now available with Word and Excel solutions when you use VSTO to build your solution. The document is a designer on which you can add controls by dragging them from the Toolbox.

There are two types of controls that can be added to the designer: Windows Forms controls and host controls. These controls are described in Chapter 8, Controls in Word and Excel.

The designer is the visual representation of the application, and each designer is associated with a code file (as mentioned earlier, this code is sometimes referred to as a code-behind file). You can switch from designer view to code view in a number of ways. One way is to right-click the document and then click View Code on the shortcut menu.

How VSTO Integrates with Visual Studio 23

When you're in code view, you can right-click the Code Editor and select View Designer from the shortcut menu to open the designer in Visual Studio, as shown in Figure 1.6. You can also right-click the code file in Solution Explorer to open a shortcut menu that gives you both the View Code and View Designer options. You'll learn more about the Code Editor in Chapter 2.

/Sheetl.vb |^~ExcelWorkbookl .xls Start Page |

j (Sheet 1 Events)

-i Startup

□ Public Class Sheet 1

Private Sub Sheet1_Startup(SyVal sender As c

End Sub p Private Sub Sh End Sub End Class

Private Sub Sheet1_Startup(SyVal sender As c p Private Sub Sh End Sub End Class

^ View Designer

Insert SnippeO

Go To Definition

Breakpoint ►

Run To Cursor

A

Cut

Copy

&

Paste

Outlining ►

al sender As al sender As

Figure 1.6. Switching to the designer from the Code Editor

Code Files in VSTO

When you create a new VSTO solution, Visual Studio creates a number of files. For example, if you create a Word document solution in Visual Studio, you will see the files represented as nodes in Solution Explorer, as shown in Figure 1.7. These files are stored in the location you indicate in the New Project dialog box. In this book, all examples should be saved at the root of your system drive, in a subdirectory named Samples. For example, if your root drive is C:\, you would save the projects in the C:\Samples directory.

x

1 Solution Explorer - Solution 'SampleDocum... ▼ Ç X

4

ffllpsll*! j a

3

Solution 'SampleDocument' (1 project) q ^ sampledocument

. Event args ]

Q ßfpi SampleDocument.doc : el hlhbbbbff :

S ^ ThisDocument.Designer.xml

^ ThisDocument.Designer.vb a My Project

: ct[i. EvcntArgg

IS- 13 References ö- £j bin ä" lj obj

Figure 1.7. Solution files displayed in Solution Explorer when Show All Files is selected

Figure 1.7. Solution files displayed in Solution Explorer when Show All Files is selected

You will typically write your code in the ThisDocument.vb source file. This file contains the ThisDocument class, which has two event handlers (ThisDocument_Startup and ThisDocument_Shutdown). You can open this file in Notepad to view its contents. Figure 1.8 shows the contents of a ThisDocument code file when you first create a Word solution using VSTO.

P. ThisDocument.vb

Notepad

^jnjxj

File Edit Format View Help

¡Public Class ThisDocument

private sub

ThisDocument.

.startup (Byval sender as object,

Byval

e as system.EventArgs) Handles Me.startup

End sub

Private Sub

ThisDocument.

_Shutdown(Byval sender As Object,

, Byval

e As System.EventArgs) Handles Me. Shutdown

End sub

End class

d

LI

Figure 1.8. Viewing ThisDocument.vb in Notepad

Figure 1.8. Viewing ThisDocument.vb in Notepad

Of course, you would not add your code to this file using Notepad, although you could if you wanted to. The ThisDocument.vb file is available to you inside Visual Studio. You can right-click this file in Solution Explorer and select View Code, as shown in Figure 1.9.

Figure 1.9. Viewing ThisDocument.vb in Visual Studio

Here you have all the rich features of the Visual Studio IDE, including IntelliSense and debugging tools to make your coding tasks easier. There are some additional files behind ThisDocument.vb, which holds the code that VSTO automatically generates. The files are ThisDocument.Designer.xml and ThisDocument.Designer.vb. You'll learn more about these files in Chapter 2.

0 0

Post a comment