Beginning to Write Code

When you write a report, you use the rules and symbols of your spoken language to tie words, sentences, and paragraphs together. The report is organized into elements such as sections and paragraphs. Each section presents a specific point, but all are tied together to support the report's main purpose. In Visual Basic programs, these elements are represented by modules and procedures—elements that you create to organize your code into logical tasks, each of which serves a specific function.

Opening a Blank Code Module

In Visual Basic for Applications, the Visual Basic Editor is your development environment, and your written code lies within a code module. You can insert as many code modules as you need to organize your code into manageable pieces that can be used in other programs or by other users. Opening a blank module is one of the first steps in writing Visual Basic code.

  1. Start Microsoft PowerPoint. In the opening PowerPoint dialog box, select Blank Presentation and click OK.
  2. In the New Slide dialog box, select the second slide layout (Bulleted List) and click OK. (The actual slide layout you select makes no difference.)
  3. On the Tools menu, point to Macro, and then click Visual Basic Editor on the submenu. (Or press ALT+F11 as a shortcut to display the visual basic editor.)
  4. In the Editor, click Module on the Insert menu.

Creating a Procedure

You can create two common types of procedures in Visual Basic: Sub and Function. A Sub procedure performs actions but doesn't return a value; a Function procedure, however, returns a value. You can use a third type, the Property procedure, to create and manipulate custom properties, but the Property procedure is beyond the scope of this book.

Sub procedures start with the keyword Sub, followed by the name of the procedure, and end with the keywords End Sub. To create a procedure, give it a unique name or header.

  1. In the Code window of the module you just opened, type Sub MyNewProcedure and press ENTER.
  2. Visual Basic automatically inserts the keywords End Sub two lines below. End Sub indicates where the procedure ends, just as a paragraph ends with a "return" in a word processor. Generally, procedures within a code module have common features that logically tie them together.

  3. Add the following lines between the line Sub MyNewProcedure and End Sub:
  4. MsgBox "This text is displayed in a " & _
        "message box"
    

    Visual Basic treats the two separate lines as one. The space and underscore ( _ ) at the end of the first line indicate that you want to continue a line of code onto the next line. This is analogous to what you do in text when you add a hyphen to continue a word from one line to the next. You use the ampersand (&) here to concatenate (join sequentially) two pieces of text. You complete each line of code by pressing ENTER at the end of the line.

    The procedure should look like the following:

    Sub MyNewProcedure 
         MsgBox "This text is displayed in a " & _
             "message box"
    End Sub
    

  5. Make sure you place the cursor within the MyNewProcedure procedure and press F5 to run it.

NOTE
When you press the F5 key, Visual Basic runs the code starting from the first line of the procedure in which the cursor is placed. Pressing F5 is equivalent to clicking Run Sub/UserForm on the Run menu in the Visual Basic Editor.

    You'll see a message box like this:

  1. Click OK to close the message box.
  2. By creating individual procedures, you break your code into distinct elements so that if your program doesn't work right, you can systematically track down problems. In addition, if you encapsulate a specific programming task in a procedure, you can more readily copy the procedure and paste it into another program with few or no changes. As you'll see in Chapter 13, "Developing COM Add-Ins," procedures that you create to add or remove menus and toolbars can be used in any Visual Basic program for Office.

Add Comments to Code

When you write Visual Basic code, you may want to include comments for a couple of reasons:

In a word processor like Microsoft Word, you usually add a comment by clicking Comment on the Insert menu. In Visual Basic, you add comments to your code by adding a single quotation mark (') at the beginning of the sentence. Visual Basic doesn't read or run anything in this line.

  1. In the same procedure you just created, move to the end of the first line Sub MyNewProcedure and press ENTER.
  2. On the new blank line, press TAB and add the following text:
  3. ' The line below will display a message box
    

    Once you type this line and press ENTER, Visual Basic colors this line of code green for easier reading. You can also add comments at the end of a line of code just by adding a single quotation mark. Unless you specify another color, green text in a code module indicates comments.

TIP
To customize the color of text in a code module, click Options on the Tools menu in the Visual Basic Editor. In the Editor Format tab, select any item in the Code Colors list box and select a new color in the Foreground drop-down list.

  1. Click at the end of the line that reads "message box" and add the following text:
  2. ' This line is continued
    

    The procedure should look like the following:

    Click to view at full size.

  3. Finally, press F5.
  4. Once again you'll see a message box. Visual Basic ignored both the comment line above the message box code and the comment attached to the end of the line of code.

  5. Click OK to close the message box.

TIP
In Visual Basic programming you commonly indent code by inserting a tab at the beginning of each line of code, between the lines Sub <Procedure> and End Sub, as shown in the preceding illustration. This greatly improves readability. To insert a tab, just press the TAB key on your keyboard.