Tải bản đầy đủ - 0 (trang)
Figure 18-6. A worksheet and the PrintTitleRows property

Figure 18-6. A worksheet and the PrintTitleRows property

Tải bản đầy đủ - 0trang

For example, if the AllowSorting property is True, then users can still sort unlocked cells in a

protected worksheet.

The AllowEditRanges property returns an AllowEditRanges object, discussed separately in the

text.



18.3.2 The AllowEditRange Object

The AllowEditRange object allows a specified range of cells on a worksheet to be password

protected from editing. Once a range has been protected in this way, and the entire worksheet has

been protected, any attempt at editing cells in that range will require the password.

Here is some code that assigns a password to a range on the active worksheet. It also demonstrates

the use of the AllowEditRanges collection.

Sub ProtectRange()

Dim ws As Worksheet

Dim i As Integer

Set ws = Application.ActiveSheet

' Remove protection

ws.Unprotect

' Delete all current protection ranges

'MsgBox ws.Protection.AllowEditRanges.Count

For i = 1 To ws.Protection.AllowEditRanges.Count

Debug.Print ws.Protection.AllowEditRanges(i).Title

ws.Protection.AllowEditRanges(i).Delete

Next

' Add a protection range

ws.Protection.AllowEditRanges.Add _

Title:="Headings", _

Range:=Range("A1:A4"), _

Password:="hide"

' Protect sheet (else protection range is not enabled)

ws.Protect

End Sub



The properties of the AllowEditRange object are:

Range

Returns or sets the range associated with the AllowEditRange object.

Title

Returns or sets the title (i.e. name) of the range associated with the AllowEditRange

object.

Users

Returns the collection of UserAccessObjects associated with the AllowEditRange object.

For more on this, see the section on the UserAccess object.



223



The methods of the AllowEditRange object are:

ChangePassword

Changes the password associated with the AllowEditRange object.

Delete

Deletes the AllowEditRange object.

Unprotect

Unprotects the workbook.



18.3.3 The UserAccess Objects

UserAccess objects allow certain users to access a protected range without requiring the password.

For instance, if your username is steve, then the following code will allow you to access protected

ranges:

Sub AddUser()

Dim ws As Worksheet

Dim ua As UserAccess

Set ws = Application.ActiveSheet

' NOTE: Sheet must be unprotected for this code to work!

ws.Unprotect

Set ua = ws.Protection.AllowEditRanges(1).Users.Add("steve", True)

End Sub



Note that the worksheet must be unprotected for this code to run without error.

The UserAccess object has but three members: the AllowEdit Boolean property, the read-only

Name property, and the Delete method.

The UserAccessList collection holds the current UserAccess objects.



18.4 Example: Printing Sheets

We can now implement the PrintSheets feature of our SRXUtils application. Recall that 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 18-7.

The list box contains a list of all sheets in the active workbook. The user can select one or more

sheets and hit the Print button to print these sheets.

Figure 18-7. Print sheets dialog



224



The steps to create the print utility are as follows: 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

SRXUtils.xla (if it is loaded) and close the workbook SRXUtils.xls (if it is open).



18.4.1 Create the UserForm

Create the dialog shown in Figure 18-7 in the Print.xls workbook. Name the dialog

dlgPrintSheets and set its Caption property to "Print Sheets." Then change the

PrintSheets procedure to:

Public Sub PrintSheets()

dlgPrintSheets.Show

End Sub



The dlgPrintSheets dialog has two command buttons and one list box:

dlgPrintSheets.Show



18.4.1.1 List box

Place a list box on the form as in Figure 18-7. Using the Properties window, set the properties

shown in Table 18-4.

Table 18-4. Nondefault Properties of the List Box

Property

Value

Name

lstSheets

TabIndex

0

MultiSelect

frmMultiSelectExtended

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.

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.4.1.2 Print button

Place a command button on the form as in Figure 18-7. Using the Properties window, set the

properties shown in Table 18-5.



225



Name

Accelerator

Caption

TabIndex



Table 18-5. Nondefault Properties of the Print Button

Property

Value

cmdPrint

P

Print

1



18.4.1.3 Cancel button

Place another command button on the form as in Figure 18-7. Using the Properties window, set

the properties shown in Table 18-6.



Name

Accelerator

Caption

TabIndex

Cancel



Table 18-6. Nondefault Properties of the Cancel Button

Property

Value

cmdCancel

C

Cancel

2

True



18.4.2 Create the Code Behind the UserForm

Now it is time to create the code behind these controls.

18.4.2.1 The Declarations section

The Declarations section of the dlgPrintSheets UserForm should contain declarations of the

module-level variables, as shown in Example 18-1.

Example 18-1. Module-Level Variable Declarations

Option Explicit

Dim cSheets As Integer

Dim sSheetNames() As String



18.4.2.2 Cancel button code

The Cancel button code is shown in Example 18-2.

Example 18-2. The cmdCancel_Click Event Handler

Private Sub cmdCancel_Click()

Unload Me

End Sub



18.4.2.3 Print button code

The Print button calls the main print procedure and then unloads the form; its source code is

shown in Example 18-3.

Example 18-3. The cmdPrint_Click Event Handler



226



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Figure 18-6. A worksheet and the PrintTitleRows property

Tải bản đầy đủ ngay(0 tr)

×