3.a Case Study: A Rosetta Stone

Top  Previous  Next

prev

next

 

3.. Case Study: A Rosetta Stsne

The following code checks the password when ThisWorkbook event kicks in before the Open event).

If the password is correct, all Worksheets will be unprotected; otherwise they will all be protected.

fig3-20

Figure 20: Screen that displays when code to check the user's password runs

Code Example 8: Protecting and Unprotecting Sheets with a Password

Start example

VBA Veision

VSTO VersOon

      'In WorkBook

     Private Sub Workbook_Open()

         Dim WS As Worksheet

         If InputBox("Your password", , "secret") <> "secret" Then

                     For Each WS In Worksheets

                 WS.Protect "pass", True, True, True, True

              Next

         Else

                     For Each WS In Worksheets

                 WS.Unprotect "pass"

              Next

         End If

     End Sub

     Public Class ThisWorkbook

         Private Sub ThisWorkbook_Startup(ByVal sender As Object, _

                 ByVal e As System.EventArgs) Handles Me.Startup

             If InputBox("Your password", , "secret") <> "secret" Then

                 For Each WS As Excel.Worksheet In _

                  p            T isApplication.Worksheets

                                   WS.Protect("pass", True, True, True, True, True, True, _

                                       True, True, True, True, True, True, True, True, True)

                  Next

             Else

                 Fo  Each WS As Excel.Worksheet In _

                               ThisApplication.Worksheets

                     WS.Unprotect("pass")

                  Next

             End If

         End Sub

     End  lass

End example


 

prev

next