Writing Select Case Decision Structures

Visual Basic also lets you control the execution of statements in your macros by using Select Case decision structures. A Select Case structure is similar to an If...Then...ElseIf structure, but it is more efficient when the branching depends on one key variable, or test case. In addition, Select Case structures make your macro code more readable for others and easier to update later. The syntax for a Select Case structure looks like this:

Select Case variable  Case value1      program statements executed if value1 matches variable  Case value2      program statements executed if value2 matches variable  Case value3      program statements executed if value3 matches variable  .  .  .  End Select 

A Select Case structure begins with the Select Case keywords and ends with the End Select keywords. You replace the variable placeholder with the variable, property, or other expression that is to be the key value, or test case, for the structure. You replace value1, value2, and value3 with numbers, strings, or other values related to the test case being considered. If one of the values matches the variable, the statements below its Case clause are executed, and Visual Basic continues executing program code after the End Select statement.

You can include any number of Case clauses in a Select Case structure, and you can include more than one value in a Case clause. If you list multiple values after a case, separate them with commas. A Select Case structure also supports a Case Else clause that you can use to control how Visual Basic handles cases not captured by the preceding cases.

The following example shows how you can use a Select Case structure in an Office macro to display an appropriate message about a person's age. If the Age variable matches one of the Case values, an appropriate message is displayed by using a message box. If not, the Else clause is displayed.

Select Case Age  Case 16      MsgBox "You can drive now!"  Case 18      MsgBox "You can vote now!"  Case 21      MsgBox "You can drink wine with your meals."  Case 65      MsgBox "Time to retire and have fun!"  Case Else      MsgBox "You're a great age! Enjoy it!"  End Select 

A Select Case decision structure is usually much clearer than an If...Then structure and is more efficient when you're making three or more branching decisions based on one variable or property. However, when you're making two or fewer comparisons, or when you're working with several different values, you'll probably want to use an If...Then decision structure.

Using Select Case to Determine a Document's Paper Size

The following exercise demonstrates how you can use a Select Case structure to display the current Word document's paper type. You can accomplish this task by using a macro to compare the PageSetup object's PaperSize property to three different Office constants associated with paper.

The PaperSize macro is located in the Chap60 document on the Running Office 2000 Reader's Corner page.

Complete the following steps:

  1. Choose Macros from the Macro submenu of the Tools menu.
  2. Select the document in which you want to store the macro using the Macros In drop-down list box.
  3. Type PageSize in the Name text box, and then click the Create button.

    Word starts the Visual Basic Editor and opens a new macro procedure named PageSize in the Code window.

  4. Type the following program statements.
  5. Dim PaperType  PaperType = ActiveDocument.PageSetup.PaperSize  Select Case PaperType  Case wdPaperLetter      MsgBox "Document type is Letter (8 1/2 x 11)."  Case wdPaperLegal      MsgBox "Document type is Legal (8 1/2 x 14)."  Case wdPaperEnvelope10      MsgBox "Document type is Envelope 10 (4 1/8 x 9 1/2)."  Case Else      MsgBox "Type unknown. Check File/Page Setup/Paper Size."  End Select 

  6. Click the Save button to save the macro to disk.

The PageSize macro requires no user input. It simply stores the current paper size in a Variant variable named PaperType and then uses a Select Case structure to determine which type of paper is in use. The results are then displayed in a message box for the user.

The default paper size in Word is Letter (8 ½-inch by 11-inch), but you can adjust this setting by using the Page Setup command on the File menu. If you're ever uncertain about the page size, just run this macro.

Occasionally, a document will use a paper type that's not accounted for in this macro. To handle this possibility, the Select Case structure uses an Else clause to display the message "Type unknown. Check File/Page Setup/Paper Size." However, you can add more functionality to your macro by adding more Case statements and paper size constants. To get a complete listing of the constants available, search for "PaperSize property" in Word Visual Basic Help.

Running the Macro

Follow these steps to run the PageSize macro and determine your document's paper size:

  1. Click the View Microsoft Word button on the Visual Basic Editor toolbar. Word displays the current document.
  2. Run the PageSize macro. Word activates the Visual Basic Editor and displays a message box that describes the current document's paper type. Before you print, you can use this information to make sure you have the right type of paper in your printer.

  3. Experiment with the macro if you like by changing the paper type using the Page Setup command on the File menu. (The Paper Size tab controls the paper type.)

Running Microsoft Office 2000 Small Business
Running Microsoft Office 2000
ISBN: 1572319585
EAN: 2147483647
Year: 2005
Pages: 228

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