Exploring Solution Explorer

Visual Studio projects are displayed and managed in a window called Solution Explorer. Solution Explorer acts as a container for all the items in your solution. For example, the solution might contain multiple projects, and each project might contain multiple items such as code files, Windows forms, or resources.

When you create a VSTO solution, Visual Studio automatically creates a project for the solution, along with a default set of associated items. For example, when you create an Excel workbook solution, Visual Studio automatically adds to your solution a class for a workbook and classes for each worksheet in the default Excel workbook. Visual Studio also adds any references that are required by VSTO. For example, a reference is automatically added to the Excel namespace (Microsoft.Office.Interop.Excel) when you create an Excel workbook or template project.

Solution Explorer in Visual Studio is similar to Project Explorer in the VBE, in that Solution Explorer displays a hierarchical view of the items in your project. Solution Explorer gives you access to all the projects and project items in your solution and enables you to navigate quickly to a particular file. You can also alternate between designer view and code view by using Solution Explorer. As described earlier, you can right-click a worksheet in Solution Explorer and select View Code from the shortcut menu to display the associated code file. If Solution Explorer is not visible, you can access it by clicking the View menu and then selecting Solution Explorer.

Visual Studio provides an alternative mechanism to Solution Explorer for navigating through the files in your project. After a file has been opened in code view, Visual Studio adds the file to Visual Studio as a tabbed window, as shown in Figure 2.12. You can then navigate to a particular file by clicking the appropriate tab. To close a tabbed window, right-click a tab and then click Close. You can use these code tabs in conjunction with Solution Explorer to help manage your project.

Sheetl.vb* Sample Workbook.xls Sheet 1.Designer.vb Start Page

Z(sh,

Save Sheetl.vb

v 11 Startup

□ Pub

Close

irtup (ByVal sender As Object, ByVal

m

Closed But This Copy Full Path Open Containing Folder New Horizontal Tab Group New Vertical Tab Group

F] Private Sub Sheetl_Shutdown(ByVal sender As Object, ByVal End Sub -End Class

F] Private Sub Sheetl_Shutdown(ByVal sender As Object, ByVal End Sub -End Class

Figure 2.12. Tabbed windows in Visual Studio

In Solution Explorer, an Excel workbook solution is at the top of the hierarchical tree and contains a project node. The project node contains the Workbook.xls file, which contains four default code files—Sheet1.vb,

Sheet2.vb, Sheet3.vb, and ThisWorkbook.vb—as shown earlier in Figure 2.11. It is in these code files that you will write the majority of your code when you customize an Excel application.

Note that the number of sheets that are created can vary if you've changed your Excel options to specify a larger or smaller number of sheets to be created in a new blank workbook. You can rename these worksheets in two ways. The first way is within the Excel application, where you right-click the tab at the bottom of the worksheet and click Rename. This action renames only the sheet name that is visible to the end user; it doesn't change the programmatic name. Figure 2.13 shows how Sheet1 was changed to MyWorksheet in the Excel tab, but the name in Solution Explorer is Sheet1.vb (MyWorksheet). If you look at the Properties window, the programmatic name is still Sheet1. When you refer to this worksheet in code, you still must refer to the programmatic name of Sheet1.

Solution Explorer Excel
Figure 2.13. Renaming a worksheet using the Excel worksheet tab

Similarly, if you use the second technique—renaming the worksheet in the Properties window—only the programmatic name of the worksheet is changed, and it doesn't affect the display name of the worksheet.

Although this might seem strange at first, it's actually a good thing. After you've deployed your solutions to end users, they are free to change the names of the worksheets; but such a change would not cause your code to fail, because the programmatic name of the sheet remains unchanged. If you want to change the display name, file name, and programmatic name so that they are all the same, you can do this by renaming the tab, changing the Name property of the worksheet, and changing the File Name property of the code file, as shown in Figure 2.14.

fit EncelWorkbookl - Microsoft Visual Studio

File. Edit View Project Build Debug Data Tools Window Community is * & ta-a m\«a an s 11 - -| ► ii fit EncelWorkbookl - Microsoft Visual Studio

File. Edit View Project Build Debug Data Tools Window Community is * & ta-a m\«a an s 11 - -| ► ii

51

ExcelWorkboi

A u

Dkl.xls* Start Page » X

Solution Explorer - ? x |

i Jlsl

y» * «a- & s - si itt ®|

I

L21

ti

E

"' I EncelWorkbookl

] jjp ExcelWorkbookl.xls

A

B

c

D

E

F

G

H

I

j-

zi

1

lJJ MyWorksheet.vb : ijJ Sheet2.vb Sheet3.vb 13] ThisWorkbook.vb m My Project

2

3

i

5

e

7

8

3

|c^Solution Explorer |(^Data Sources 1

10

[Properties » ? x |

11

12

MyWorksheet.vb File Properties » 1

13

0! 0;

nEJlel

14

15

Copy to Output 1 Do not copy Custom Tool Custom Tool Nan Display Name My Worksheet

16

17

16

19

26

21

j

File Name

Name of the file.

22

MJ

► M\MyWorksheet ¿sheets /_S

heet3 /

M

I HI

Figure 2.14. Renaming a worksheet's file name, display name, and programmatic name

Figure 2.14. Renaming a worksheet's file name, display name, and programmatic name

There is only one default code file for Word and Outlook solutions. The default code file in Word is ThisDocument.vb, and the default code file in Outlook is ThisApplication.vb. You'll notice that there is also a My

Project node within the project. The My Project folder gives you access to the properties for the active project and contains information about any project resources.

There are additional (hidden) code files behind each of the class files, and these files are visible only when you click Show All Files in Solution Explorer. Figure 2.15 shows a VSTO Excel solution in Solution Explorer that displays only the default code files (left) and an Excel solution that displays all the code files (right).

¿^Solution Explorer j^Data Sources

Figure 2.15. Code files in Solution Explorer

¿^Solution Explorer j^Data Sources

Figure 2.15. Code files in Solution Explorer

The additional files listed under each class have the same name as the class but a different extension. For example, Sheetl.vb contains a Sheetl.Designer.xml file and a Sheetl.Designer.vb file. There is a ".Designer.xml" and a ".Designer.vb" file for each workbook and worksheet in the project. The .Designer.xml files are read-only and provide information about programmable objects on the worksheet or in the workbook; these files should not be modified. The Designer.vb files contain auto-generated code.

In VBA, lots of code runs automatically behind the scenes; you concern yourself only with the code that you personally write. In Visual Basic 2005, this auto-generated code is exposed through these hidden files. The idea is that the code you write is separate from the code that gets automatically generated. For example, the initialization code that is generated when you add a button to a document need not be mixed in with the code that you write to handle the button's Click event handler.

Looking at the code in these files can be a great learning tool, but you should not modify it in any way. This is because these code files get regenerated, and any changes you've made will likely get overwritten. For example, if you want to write code to customize Sheetl, you should add the code to the Sheetl.vb source file; you should not modify the Sheetl.Designer.vb source file, because this code will get overwritten when the file is regenerated.

Table 2.l describes the nodes displayed in Solution Explorer for a Word solution named SampleDocument.

Table 2.1. Solution Nodes Displayed in Solution Explorer

Node

Description

Solution 'Sample Document'

The top-level node that represents the solution. A solution is a file used by Visual Studio to manage one or more related projects. In this example, the solution is named SampleDocument. This corresponds to the SampleDocument.sln file saved in the C:\Samples directory.

SampleDocument

The project. In this example, the project is also named SampleDocument. This corresponds to the SampleDocument.vbproj file saved in the C:\Samples directory.

SampleDocument.doc

The Word document that you are customizing.

(continues)

(continues)

Table 2.1. Solution Nodes Displayed in Solution Explorer (Continued)

Node

Description

ThisDocument.vb

The code file that corresponds to the ThisDocument.vb file saved in the C:\Samples directory.

ThisDocument.Designer.xml

An XML file that contains information about controls that are added to the document. This file is visible only when Show All Files is clicked in the toolbar in Solution Explorer. You should not edit this file.

ThisDocument.Designer.vb

The code file that contains the auto-generated code created by the designer. This corresponds to the ThisDocument.Designer.vb file saved in C:\Samples. This file is visible only when Show All Files is clicked in the toolbar in Solution Explorer. You should not edit this file.

MyProject

Contains project settings, such as resources for the solution.

References

Contains references to other managed assemblies or COM type libraries. This file is visible only when Show All Files is clicked in the toolbar in Solution Explorer.

Bin

Contains the compiled solution assembly generated from the code-behind files, which contain your customizations. Also contains related files, such as the Word document that uses the customizations in the compiled assembly. This file is visible only when Show All Files is clicked in the toolbar in Solution Explorer.

Obj

Contains the files that Visual Studio uses to link and create the binary output for a project. This file is visible only when Show All Files is clicked in the toolbar in Solution Explorer.

Similar files are created for Excel and Outlook solutions. For Outlook solutions, the ThisApplication.vb node contains a ThisApplication.Designer.xml file and a ThisApplication.Designer.vb file. In an Excel solution, there is a ThisWorkbook.vb and three worksheet files (Sheet1.vb, Sheet2.vb, and Sheet3.vb). There is a corresponding .Designer.xml and .Designer.vb for each of these files in Excel projects. In VSTO 2005 SE add-ins, a ThisAddin.vb node contains a ThisAddin.Designer.xml file and a ThisAddin.Designer.vb file.

+1 0

Responses

  • diamond
    What is the solution explorer in vba?
    11 months ago

Post a comment