The My Objects

The new My objects give you easy access to application, system, and user information without having to search through the .NET Framework class libraries to locate equivalent functionality. Instead, if you type My followed by a period, IntelliSense displays all the members of the My objects that are available to VSTO solutions. Table 4.1 describes these objects as well as those that are not available.

Table 4.1. The My Objects in Visual Basic 2005

My Object



Enables you to access information about the application. For example, you can change the culture of your application to English (US) by using My.Application.ChangeCulture("en-US").


Enables you to set properties for computer components such as the file system and the Clipboard. For example, you can clear the Clipboard by using My.Computer.Clipboard.Clear(). You can also retrieve information about the computer, such as its name or operating system.


Provides read-only access to resources in your application. For example, you can retrieve a string resource by referencing the name of the string. If you have a string resource named ControlName, using My.Resources.ControlName retrieves the value of the string.


Provides access to application settings. For example, you can reset the application settings to their default values by using My.Settings.Reset().



Table 4.1. The My Objects in Visual Basic 2005 (Continued)

My Object



Gives you access to information about the current user. For example, you can check the name of the current user by using My.User.Name.


Provides an instance of every Web service that is currently referenced in the project. You can call a function within a Web service by using My.WebServices.


Not available in VSTO projects.


Not available in VSTO projects.


Not available in VSTO projects.


Not available in VSTO projects.

One example of using a My object is to access the name of the current user. Note, however, that VSTO does not set My.User by default. You must first call InitializeWithWindowsUser. The code in Listing 4.1 displays the current user in a message box.

Listing 4.1. Displaying the user's login name in a message box My.User.InitializeWithWindowsUser()


In this section you will create a simple Word application that demonstrates some of the My object features.

1. On the File menu, point to New and then click Project.

2. Select a Word document in the Templates pane, leaving all the default settings in the New Project dialog box. Click OK to create a new document project.

3. In the Visual Studio Tools for Office Project Wizard, select Create a New Document, and then click OK.

4. Drag a combo box and list box from the Toolbox to the document, and resize the controls so that it looks similar to the document shown in Figure 4.1.

Figure 4.1. Document with combo box and list box controls

5. In Solution Explorer, right-click ThisDocument.vb and select View Code.

The Code Editor opens and displays two default event handlers: Startup and Shutdown.

6. Replace the code in the Startup event handler with the code in Listing 4.2. This code adds entries to the combo box when the document first opens.

Listing 4.2. Adding entries to the combo box

Private Sub ThisDocument_Startup(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles Me.Startup

5tart Page ThisDocurnent.yb* SampleDocument.doc*

5tart Page ThisDocurnent.yb* SampleDocument.doc*

Figure 4.1. Document with combo box and list box controls

With Me.ComboBox1.Items .Add("Ports") .Add("Drives")

.Add("Loaded Assemblies") End With

End Sub

7. Add the code in Listing 4.3 to the ThisDocument class. This code uses the My objects to populate the list box with information about the system drives and ports, along with the assemblies that are loaded when the application is running.

Listing 4.3. Using the My objects to obtain system information

Private Sub ComboBox1_SelectedValueChanged(ByVal sender _ As Object, ByVal e As System.EventArgs) Handles _ ComboBoxl.SelectedValueChanged

Select Case ComboBoxl.Selectedltem Case "Loaded Assemblies" Dim i As Integer = 0

Dim TotalAssemblies = My.Application.Info. _

LoadedAssemblies.Count Me.ListBox1.Items.Clear() For i = 0 To TotalAssemblies - 1

Me.ListBox1.Items.Add(CStr(My.Application.Info. _ LoadedAssemblies(i).FullName))

Next Case "Drives"

Dim i As Integer = 0

Dim TotalDrives = My.Computer.FileSystem.Drives.Count Me.ListBox1.Items.Clear()

Me.ListBox1.Items.Add(CStr(My.Computer. _ FileSystem.Drives(i).Name))

Next Case "Ports"

Dim i As Integer = 0

Dim TotalPorts = My.Computer.Ports. _

SerialPortNames.Count Me.ListBox1.Items.Clear()

Me.ListBox1.Items.Add(CStr(My.Computer. Ports.SerialPortNames(i)))

Next End Select End Sub

8. Run the code by pressing F5.

The code is built and the Word application opens. When you select Ports in the combo box, the list box displays the name of the ports on your system. When you select Drives, the list box displays all the available drives. The Loaded Assemblies option displays all the assemblies that the application has loaded on your system, as shown in Figure 4.2.

Figure 4.2. Displaying the assemblies loaded by the application
0 0

Post a comment