Using the Visual Basic Editor


One of the major components of VBA is the Visual Basic Editor. The editor is the development environment you use when writing, editing, and debugging code. It is also where you create forms ( UserForms in the context of VBA) for interacting with the users of your code, and create references to external objects and libraries.

The discussion in this section is focused on just two aspects of the Visual Basic Editor: the windows and the Tools menu. Other elements of the editor are described as part of larger discussions later in the chapter.

Tip  

Visual Basic and programming terminology       Standard Visual Basic and general programming terms are italicized the first time they are discussed in this chapter.

Windows

When you open the editor ”either by clicking Tools, Macro, Visual Basic Editor, or by pressing Alt+F11 ”just two windows are visible:

  • The Project Explorer shows all the modules, UserForms, and other items in your VBA project, including references to other projects.

    Note  

    Unless otherwise noted, in this chapter the term "project" means VBA project, whereas the term "plan" is used to refer to Microsoft Project files.

    When you open the editor, two VBProject objects are listed. The first object, the ProjectGlobal project, contains all the code, UserForms, and modules associated with the Global file. The second object, the VBAProject project ("VBAProject" is the default name of any new project), contains the same items associated with the open Microsoft Project plan.

    Initially, the This Project object is selected in the Project Explorer. This Project represents the plan itself, including events associated with the plan (see Figure 31-1).


    Figure 31-1: View all the items in your project by using the Project Explorer.

    Cross-References  

    For more information about plan-level events, see "Working with Events" later in this chapter.

  • The Properties window lists the properties for the selected item (see Figure 31-2). You can use the Properties window to easily change design-time properties, such as the name or style of an object, which can only be modified by using the editor.


    Figure 31-2: The Properties window shows the properties of the selected project and each of the items (UserForms, modules, and so on) that are contained in it.

    Tip  

    Run-time properties       Some of the properties listed in the Properties window are also run-time properties, which means they can be changed by your code while it is running.

Also of major importance is the Code window, in which you spend the majority of your time when using the editor. All Visual Basic code attached to a plan or a UserForm, or in a module or class, appears in the Code window. To open the Code window, simply double-click an item in the Project Explorer. (To view code for a UserForm, right-click and then click View Code.)

There are four controls associated with the Code window:

Objects box .       For all items in the Project Explorer, this box displays the word (General). For VBProject objects and UserForms, it also displays the name of the object (Project or UserForm). Finally, for UserForms, the Objects box also displays the name of each control on the UserForm.

Procedures/Events box .       For all items in the Project Explorer, this box displays the word (Declarations) and the names of procedures. For VBProject objects and UserForms, it also displays the names of events associated with the object selected in the Objects box.

Procedure View button .       Displays just one procedure at a time in the Code window. This button is located in the lower-left corner of the Code window.

Full Module View button .       Displays all procedures in the Code window, each separated by a horizontal line. Full Module is the default view for the Code window, and it is also located in the lower-left corner of the Code window.

The following windows are also part of the editor, but will be discussed in more detail later in this chapter:

Immediate .       Any code entered in this window is immediately evaluated and run. Code entered in the Immediate window is not saved. It is frequently used as a sort of scratch pad, especially when debugging.

Locals .       Displays information about all local (procedure) variables and their values. Used when debugging.

Watch .       Displays information about watches that have been created. A watch is any expression that can be evaluated to produce a value. Used when debugging.

UserForm .       Displays a UserForm. Used when creating or editing the visual elements of a UserForm.

Toolbox .       Displays controls that can be placed on a UserForm, including ActiveX controls and other insertable objects you might have added to your project from external sources. Used when creating or editing the visual elements of a UserForm.

Object Browser .       Displays information about the objects, properties, methods , events, and constants (the object model) that are available in type libraries referenced by your project or that you create in procedures. (Type libraries are files with the .olb, .tlb, or .dll extensions.) Used when writing code or learning about the features of an object model.

Tools Menu

One of the most powerful functionalities of the VBA environment is its capability to extend itself beyond the bounds of the host program. Two of the commands that provide this functionality, adding references to other object models and additional controls for UserForms, are found on the Tools menu. Also on the Tools menu are the following commands for customizing the editor and adding Help files, password protection, and digital signatures to your project:

References.       Opens the References dialog box (see Figure 31-3), in which you make connections to external libraries, including the object models of other applications.

click to expand
Figure 31-3: The References dialog box controls which objects are available to your project. Selections here determine which object models display in the object browser.

Libraries that are referenced by a project are indicated by the check box next to the library name. Clearing the check box of any library that you are not using in your project results in faster compilation of your project because unused object references must otherwise be verified .

Note  

The "Visual Basic for Applications" and "Microsoft Project 11.0 Object Library" libraries are always referenced by Microsoft Project and cannot be removed.

The Priority buttons change the order in which libraries are searched for objects. You should change the priority of a reference only if the same object name is used in more than one referenced library.

Note  

References are specific to each project.

Additional Controls.       Opens the Additional Controls dialog box (see Figure 31-4), in which you select additional custom controls to use on your UserForms. This command is available only when creating or editing the visual elements of a UserForm.

click to expand
Figure 31-4: Selections made in the Additional Controls dialog box determine which controls appear in the Toolbox window.
Note  

Although any control that appears in the Available Controls list can be added to the Toolbox, not all controls are authored so that they can be used outside their intended environment.

Options.       Opens the Options dialog box (see Figure 31-5), in which you select visual, behavior, and other options for the editor that suit your needs or preferences.

click to expand
Figure 31-5: Selecting the Require Variable Declaration check box on the Editor tab of the Options dialog box enforces explicit declaration of variables.

VBAProject Properties.       Opens the Project Properties dialog box (see Figure 31-6). Don't confuse the Project Properties dialog box with the Properties window. You can use the Project Properties dialog box to modify the properties of a VBProject object, including giving it a description and connecting it to a Help file.

click to expand
Figure 31-6: Specifying a password without also selecting the Lock Project For Viewing check box protects the Project Properties dialog box only.
Tip  

Project information in the object browser       The project name and description also appear in the object browser, which can be especially useful when searching for specific procedures in larger projects.

You can prevent other people from viewing or modifying your project by clicking the Protection tab, selecting the Lock Project For Viewing check box, and then providing a password.

Note  

The name of this command changes according to the name of the selected project. "VBAProject" is the default name of any new project.

Digital Signature.       Displays the Digital Signature dialog box (see Figure 31-7), in which you can digitally sign a project with a certificate.

click to expand
Figure 31-7: The Digital Signature dialog box for a project signed with a certificate created by using SelfCert.exe is shown here.

Digitally signing a project lets other users of your project know that it originated with the signer and has not changed since it was signed. You can use the SelfCert program, found in the Program Files\Microsoft Office\Office11 folder, to create your own certificate.

Cross-References  

For more information about digital signatures, including how to obtain a digital certificate from a commercial Certificate Authority, search for "About digital signatures" in Microsoft Project Help.

Note  

In an environment in which the macro security level is set to High on all computers, a self-signed project can be deployed but cannot be run because it does not have a secure-enough certificate. You must use a certificate issued by a Certificate Authority in this situation.

Tip  

Deploy a project under High security       One way to deploy a project in a High security environment is to send the source code to users as text, instruct them how to paste it into the Visual Basic Editor, and then use SelfCert to certify the code using their own local certificate.

start sidebar
Finding the Right Help

Microsoft Project includes four different Help systems for VBA, and it might not always be clear where the content you're reading originated. Sometimes, you might know which Help system contains the information you need, but you might not be able to figure out how to open that specific Help file.

To get assistance with

Do the following

General Visual Basic concepts and language reference

Click Help, Microsoft Visual Basic Help in the editor.

Concepts and language reference common among Microsoft Office and related programs (including Microsoft Project)

Click a Microsoft Office “specific item in code or in the object browser and then press F1.

Members of the Microsoft Project object model

Click a Microsoft Project “specific item in code or in the object browser and then press F1.

UserForms

Click a UserForm, a control on a UserForm, or an item in the Properties window, and then press F1.

end sidebar
 



Microsoft Office Project 2003 Inside Out
Microsoft Office Project 2003 Inside Out
ISBN: 0735619581
EAN: 2147483647
Year: 2003
Pages: 268

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