Tải bản đầy đủ - 0trang
Example 21-3. Code in the Chart Sheet's Code Module
bPause = Not bPause
This code contains three procedures. The main procedure is ScrollChartTypes, which sets the
chart type and adjusts the chart's title accordingly. The procedure uses a text file, ChartTypes.txt,
that contains a list of all chart types and their names. The contents of that text file are shown in
Example 21-4. This file will need to be in the same directory as the workbook.
Example 21-4. The ChartTypes.txt File
Note the use of the On Error line in ScrollChartTypes, which resumes execution in case
we try to set the chart type to a value that is not acceptable for the particular chart.
The Delay procedure simply waits for the prescribed number of seconds. Finally, the
MouseDown event changes the state of the module level Boolean variable bPause. When the left
mouse button is clicked, scrolling is paused until the mouse button is clicked again. To stop the
procedure completely, just hit Ctrl-Break.
21.18 Example: Printing Embedded Charts
We can now implement the PrintCharts feature of our SRXUtils application. This is designed
to provide a list of the embedded charts in the active workbook, so the user can select from this list
and print the selected charts. (To print a chart sheet, use the PrintSheets utility.)
Implementing the PrintCharts utility is similar to implementing the PrintSheets and
PrintPivotTables utilities, which we did earlier in the book. At the present time, this print
utility, located in the Print.utl add-in, simply displays a message box. To implement this feature,
we want the utility to first display a dialog box, as shown in Figure 21-32.
Figure 21-32. Print Charts dialog
The list box contains a list of all embedded charts in the active workbook. The user can select one
or more charts and hit the Print button.
The following are the steps to create the print utility. All the action takes place in the Print.xls
workbook, so open this workbook. When the changes are finished, you will need to save Print.xls
as Print.utl as well. If Print.utl is loaded, the only way to unload it is to unload the add-in
SRXUlils.xla (if it is loaded) and close the workbook SRXUtils.xls (if it is open).
21.18.1 Create the UserForm
Create the dialog shown in Figure 21-32 in the Print.xls workbook. Name the dialog
dlgPrintCharts, change its caption to "Print Charts," and change the PrintCharts
Public Sub PrintCharts()
The dlgPrintCharts dialog has two command buttons and one list box.
220.127.116.11 List box
Place a list box on the form as in Figure 21-32. Using the Properties window, set the properties
shown in Table 21-9.
Table 21-9. Nondefault Properties of the ListBox Control
The MultiSelect property is set to frmMultiSelectExtended so that the user can use the
Control key to select multiple (possibly nonconsecutive) entries and the Shift key to select
multiple consecutive entries.
The TabIndex property determines not only the order in which the controls are visited as the user
hits the Tab key, but also determines which control has the initial focus. Since we want the initial
focus to be on the list box, we set its tab index to 0.
18.104.22.168 Print button
Place a command button on the form as in Figure 21-32. Using the Properties window, set the
properties shown in Table 21-10.
Table 21-10. Nondefault Properties of the Print Button
22.214.171.124 Cancel button
Place another command button on the form as in Figure 21-32. Using the Properties window, set
the properties shown in Table 21-11.
Table 21-11. Nondefault Properties of the Cancel Button
When the Cancel property of the cmdCancel button is set to True, the button is "clicked" when
the user hits the Escape key. Thus, the Escape key will dismiss the print dialog.
21.18.2 Create the Code Behind the UserForm
Now it is time to create the code behind these controls.
126.96.36.199 The Declarations section
The Declarations section of the UserForm should contain declarations of the module-level
variables, as shown in Example 21-5.
Example 21-5. Module-Level Declarations in the UserForm's Declarations Section
Dim cCharts As Integer
Dim sChartObjNames() As String
Dim sSheets() As String
188.8.131.52 Cancel button code
The Cancel button code is shown in Example 21-6.
Example 21-6. The Cancel Button's Click Event Handler
Private Sub cmdCancel_Click()
184.108.40.206 Print button code
The Print button calls the main print procedure and then unloads the form, as shown in Example
Example 21-7. The cmdPrint_Click Procedure
Private Sub cmdPrint_Click()
220.127.116.11 The Form's Initialize event