Wizards are common features of many software applications. Not only do they guide you through a series of tasks and often help you save time creating documents, they also step you through many prebuilt content templates and provide you with ideas, starter text, formatting, and organization. The new style of Office wizards now lets you navigate more easily, both backward and forward, through the steps and allows you to track your progress visually through the task steps so you have a sense of where you are and where you're going.
It's even easier if you integrate the Office Assistant with wizards. The Assistant gives you tips for going through the steps as well as visual examples and step-by-step instructions for specific tasks.
The wizard dialog box has three main features: navigation buttons, a subway map, and tab pages. The subway map is optional but is provided in many Office wizards. Your wizard works equally well without it.
The Energy Wizard has the same look and style as the Fax Wizard in Word. To access the Fax Wizard, click New on the File menu in Word, and in the Letter And Faxes tab, select the Fax Wizard icon and click OK. (The details of the AutoContent Wizard in PowerPoint differ slightly from those of the wizards in Word, but the look and style of the two wizard types are the same.) In this chapter, you'll use the details of the Fax Wizard in Word as the model. The code for the wizard UserForm will be very generic so that you can easily plug the same code into a wizard in any Office application.
You can easily build dialog boxes with multiple steps by using the MultiPage control. The MultiPage control is a container of controls within a collection of pages. As you'll see, the MultiPage control makes it simple to design each page because you can add controls to one page and display the controls of another page.
MultiPage control
This adds a new page to the mpgSteps control. The tab caption of the new page is Page3.
Your MultiPage control has three pages. The first page is the Start screen, or the screen that the wizard displays first. The last page is the Finish screen, which the wizard displays once the user completes all the steps in the wizard. The page between the first and last pages will become the step in the wizard where the user sets options and enters information that the wizard needs to complete its overall task. Each subsequent step in the wizard requires its own page. In the section later in this chapter titled "Add a New Step in the Wizard," you'll learn how to add another new page and, hence, another new step in your wizard.
By default, the program sets the style of the MultiPage control to the enumeration value fmTabStyleTabs, which represents a value of 0. Later in this chapter, when you start adding controls to each page in the control, you'll set the Style property of the MultiPage control to fmTabStyleNone so that the tabs aren't displayed in the wizard dialog box. Some wizards display tabs, but the style you're creating in this chapter doesn't.
Most well-designed wizards provide a set of common navigation buttons along the lower edge of the wizard dialog box. These navigation buttons are Cancel, Previous, Next, and Finish. With the introduction of the Office Assistant, you can add another button that allows you to display tips or instructions from the Assistant.
NOTE
By double-clicking a control in the Toolbox, you can add more than one of a particular control to a UserForm without having to continuously click the control in the Toolbox. You can't resize or select any other control in the UserForm until you click the Select Objects arrow in the Toolbox.
Select Objects arrow
Control | Property | Value |
---|---|---|
CommandButton1 | Name | cmdCancel |
CommandButton1 | Caption | Cancel |
CommandButton1 | Cancel | True |
CommandButton2 | Name | cmdPrevious |
CommandButton2 | Caption | < Previous |
CommandButton2 | Accelerator | P |
CommandButton3 | Name | cmdNext |
CommandButton3 | Caption | Next > |
CommandButton3 | Accelerator | N |
CommandButton4 | Name | cmdFinish |
CommandButton4 | Caption | Finish |
CommandButton4 | Accelerator | F |
You should set the Cancel property of the Cancel button to True so that the user can press ESC to cancel the wizard dialog box. Allowing the user to cancel a dialog box by pressing ESC is a standard Windows programming practice.
mpgSteps.Value = mpgSteps.Value - 1 |
The Value property of the MultiPage control mpgSteps is an integer that indicates the currently active page. A value of 0 represents the first page in the control. The maximum value of this property is the number of pages minus 1. Each time you click the Previous button, the program decreases the value of the mpgSteps control by 1 and displays the previous step.
mpgSteps.Value = mpgSteps.Value + 1 |
Each time you click the Next button, you increase the value of the mpgSteps control by one and display the next step.
Unload Me |
The Visual Basic keyword Me is an implicit reference to the UserForm in which the code is currently running. Thus, Me represents the frmWizard UserForm. When you click the Cancel button or press ESC, you close the dialog box and unload it from memory.
Unload Me |
For now, the Finish button closes the UserForm. Later in this chapter, however, to the cmdFinish_Click event procedure you'll add code that checks the values entered in the steps in the wizard and then calls a number of procedures to perform the wizard's overall task before unloading the dialog box from memory.
Select Case mpgSteps.Value Case 0 cmdNext.Enabled = True cmdPrevious.Enabled = False cmdFinish.Enabled = False Case m_iTotalSteps - 1 cmdNext.Enabled = False cmdPrevious.Enabled = True cmdFinish.Enabled = True Case Else cmdNext.Enabled = True cmdPrevious.Enabled = True cmdFinish.Enabled = False End Select |
When the value of the MultiPage control mpgSteps is changed by the cmdNext_Click or cmdPrevious_Click event procedure, the Change event of the mpgSteps control is triggered. The Select Case statement you added evaluates the current value of the mpgSteps control and determines the appropriate course of action. If the value is 0, the program displays the Start page and disables the Previous button because there are no more pages before the Start.
If the value is equal to the module-level variable m_iTotalSteps minus one, the program displays the Finish page and disables the Next button because there are no more pages after the Finish. In this case, however, it enables the Finish button. The variable m_iTotalSteps is declared in step 10 and represents the total number of pages in the mpgSteps control. If the value of the mpgSteps control is neither the first nor last page, both the Previous and Next buttons are enabled.
m_iTotalSteps = mpgSteps.Pages.Count mpgSteps.Value = 0 Call mpgSteps_Change |
Before the dialog box is displayed, you need to initialize the state of some controls and then set the value of the variable m_iTotalSteps. This is done in the UserForm_Initialize event procedure, which runs just before the dialog box is displayed on the screen. In the Initialize event procedure of the frmWizard UserForm, m_iTotalSteps is set to the number of pages in the mpgSteps control.
The second line of code above sets the value of the mpgSteps control to 0 so that the Start page is shown when the UserForm is displayed. Despite the fact that the value of the mpgSteps control is set, the mpgSteps_Change event procedure may not run if the value of the mpgSteps is 0 while you're designing the UserForm. Thus, you explicitly call the mpgSteps_Change event procedure so that the navigation button is also initialized.
Dim m_iTotalSteps As Integer |
TIP
If the Option Explicit statement isn't included at the beginning of the frmWizard Code window, you should add it by typing "Option Explicit" before the module-level declaration you added in the previous step. If you want the Option Explicit statement to be added to any code module by default, on the Tools menu in the Visual Basic Editor, click Options, and then click Require Variable Declaration in the Editor tab of the Options dialog box. See "Do I Need to Declare My Variables and Constants?" in Chapter 2 for more information.
The program disables the Finish and Previous buttons and shows the first page (the Start page) in the MultiPage control when the wizard is displayed. Click the Next and Previous buttons to see the pages of the MultiPage control being activated and the navigation buttons being enabled and disabled.
This example gave you the basics of setting up a wizard dialog box. In steps 12 and 13, you exported the UserForm so that you can use it as a starting point when you create a new wizard. In the next example, you'll continue building onto your wizard by adding the subway map panel on the left side of the UserForm.
The "subway map" is a new style of Office wizard that permits easy navigation through the wizard's steps by giving a visual representation of where you are within the wizard. Each "station" on the subway map represents a step in the wizard, and the current station, or step, is green. If you want to skip a step or jump to a specific step, you can do so by using the subway map panel.
Image control
If the imgStep1 and imgFinish controls are underneath the mpgSteps control, select the selection border around the mpgSteps control and move the control temporarily out of the way. Also, if one image is underneath the other, select one by clicking its name in the drop-down list in the Properties window to select the control on the UserForm. Then drag the control to a new location on the UserForm.
Property | Value |
---|---|
Name | lblStart |
BackStyle | 0 - fmBackStyleTransparent |
Caption | Start |
ForeColor | &H00FFFFFF& (or white in the Palette tab) |
Make sure the label controls are slightly wider than the text they contain.
You've completed the layout of the subway map. What remains is to add the code that allows you to use the map to navigate through the steps.
The subway map has to indicate which step is currently active and which steps you have visited. You indicate the currently active step by setting two properties of the controls in the subway map. First, set the BackColor property of the Image control, or station, to light green. Second, set the font of the station label to bold. Once you move on to another step in the wizard, the program sets the BackColor property of the Image control of the previous station to dark gray to indicate that you have previously visited it.
View Code
Dim m_sCurStep As String |
The variable m_sCurStep is the name of the current step, and it's used to keep track of the current step. Once you move to another step, you can use the value in the variable m_sCurStep to determine what the previous step was and to reset its properties so that the previous step appears visited.
SetSubwayMap |
Every time the mpgSteps_Change event procedure runs, the SetSubwayMap procedure is called so that the images and labels of the map can be initialized.
Sub SetSubwayMap() Controls("img" & m_sCurStep).BackColor = &H808080 Controls("lbl" & m_sCurStep).Font.Bold = False Select Case mpgSteps.Value Case 0 m_sCurStep = "Start" Case m_iTotalSteps - 1 m_sCurStep = "Finish" Case Else m_sCurStep = "Step" & mpgSteps.Value End Select Controls("img" & m_sCurStep).BackColor = vbGreen Controls("lbl" & m_sCurStep).Font.Bold = True End Sub |
The SetSubwayMap procedure performs three tasks. The first task is to use the m_sCurStep value to determine what the previous step is. The program calls the SetSubwayMap procedure when the value of the mpgSteps control has been changed. Before you update the value of m_sCurStep to reflect the current step, you reset the control properties of the previous step so that it appears visited. You do this by setting the BackColor property of the Image control to dark gray (&H808080) and the font of the Label control to roman (not bold).
For the second task of the procedure, the Select Case statement evaluates the current value of the mpgSteps control and sets the variable m_sCurStep to the name of the current step. You then use the value of m_sCurStep to set the BackColor property of the current Image control to light green (vbGreen). The Visual Basic color enumeration vbGreen represents the value &HFF00.The third task of the procedure is to set the associated Label control's font to bold.
m_sCurStep = "Start" |
The UserForm_Initialize event procedure should now look like this:
Private Sub UserForm_Initialize() m_iTotalSteps = mpgSteps.Pages.Count m_sCurStep = "Start" mpgSteps.Value = 0 mpgSteps_Change End Sub |
View Object
The color red is used as an initial indicator that this step is the last one in the wizard (or the last station on the subway map).
Click the Next and Previous buttons to see the program activate the pages of the MultiPage control, enable and disable the navigation buttons, and update the properties of the subway map's Image and Label controls. The last remaining task is to add the code that allows you to click the controls on the subway map to navigate between the steps.
mpgSteps.Value = 0 |
mpgSteps.Value = 0 |
mpgSteps.Value = 1 |
mpgSteps.Value = 1 |
mpgSteps.Value = m_iTotalSteps - 1 |
mpgSteps.Value = m_iTotalSteps - 1 |
Click the Image and Label controls on the subway map to see the program enable and disable the Previous and Next buttons and activate the pages of the MultiPage control.
You need to use the naming convention for the controls of the subway map so that a new step can be added easily. The following example shows how to set up your code to add more steps as needed.
mpgSteps.Value = 2 |
mpgSteps.Value = 2 |
You've completed the wizard UserForm with its subway map. In step 15, you exported the UserForm so that you can use it as a starting point when you create a new wizard with a subway map. The final process in setting up the wizard UserForm is to add the new element that all wizards in Office can include: the Office Assistant.
NOTE
Because none of the code in the UserForm you've created so far is specific to any Office application, you can import the wizard UserForm into a Visual Basic for Applications project in any application.