Code View

The windows that are visible may vary according to your settings, but you can view any of the windows by selecting it in the View menu in Visual Studio. There are many ways you can change to code view. One way is to select Code from the View menu. Another way is to right-click the code file in Solution Explorer and then click View Code. A third way is to right-click the document or worksheet and select View Code, as shown in Figure 2.10.

This action changes the view in Visual Studio so that you are now viewing the code in the Sheetl.vb class. Two methods are visible: Sheet1_Startup and Sheet1_Shutdown. You can add code to the Startup method, which handles the Startup event.

1. As shown in Figure 2.11, add the following code to the Startup method: MsgBox("Hello world!")

When you build and run the project, this code will run as soon as initialization of the document is complete.

40 SampteWwkbook - Microsoft Visual studio

File Edit View Prefect Biild D«bug Date Took insert Format Window Cmrnurity Nelt

^mplrWnrktiiHik.id^ Chri+

'10' BIT] = =

Al * f*

A

B C

0

E

F

G

H

1

2

yiew CodepY

3

Manage Named Range«. Cut

1

*

5

6

-J

Copy

7

Paste Special...

s

S

10

pi sert...

11

Delete...

12

13

_J

Qeor Contents

14

Insert Comment

15

IS

S

Fermât Cels...

17

Pick From Drop-down List...

1a

£re3teLiA...

I?

20

%

üypetlriV,,,

Î1

&

LookMp.,.

22

_

m

She

<1

M 1 ¥ M

Et 11 sheets / 5heet3 /

Figure 2.10. Changing to code view

LjicelWoikbookl - Microsoft Visual Studio

^□jxj

File

Edit View Project Build Debug Data Tools Window Community Help

Üj

n «

^i-ai * 3a | a I'M 11 ' ' ► 10 J 5I

Ci,

■ =

?

/Sheet l.vb* ExcelWorkbookl.xls -'Start Page

. X

^¡i^iffjj-sBitos/ 1 ^ ¡I'M

R7

Sheet 1 (Declarations)

-

a 1 ^ H | m A

_

¡¿¡3 EncelWorkbookl

1

3 Public Class Sheetl

-

□ or^ ExcelWorkbookl.xls 4J] Sheetl.vfa

1

] Private Sub Sheetl_Startup(ByVal sender As Object, MsgBox["Hello World") End 5ub|

ByVal e As System.Eve

Hi My Project

1

] Private Sub Sheet1 Shutdown(ByVal sender As Object

, ByVal e As System.Ev

^Solution Explorer |^Data Sources

End Sub

Properties » ^ x |

Sheetl Attributes

1-End Class

1|U

COM Class False

M COM Visible False fj|

Default Event (None) 1

CDM Class

-

Expose Class to COM.

A

1 ti

Ready

Ln 6 Col 12

Ch 12 IN5 ^

Figure 2.11. Adding code to the Startup event handler of Sheet1

❖ Note Although you can display a message box in Visual Basic using MsgBox("Hello world") as you can with VBA, you can also use MessageBox.Show("Hello world").

2. Press F5 to run the code. When the Excel workbook opens, a message box displays the text "Hello world!"

3. Close the Excel workbook.

Now that you're familiar with creating a simple solution, we can take a closer look at the various features of VSTO and the Visual Studio IDE. First, we describe the features you're probably already familiar with from your days of writing VBA code in the VBE, pointing out any differences and enhancements. These include Solution Explorer, Object Browser, the Properties window, IntelliSense, and various debugging windows. Then you will learn about Visual Studio 2005 features that you might be less familiar with because similar tools do not exist in the VBE.

0 0

Post a comment