1<?xml version="1.0" encoding="UTF-8"?> 2<!DOCTYPE script:module PUBLIC "-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "module.dtd"> 3<script:module xmlns:script="http://openoffice.org/2000/script" script:name="Protect" script:language="StarBasic">REM ***** BASIC ***** 4Option Explicit 5 6Public PWIndex as Integer 7 8 9Function UnprotectSheetsWithPassWord(oSheets as Object, bDoUnProtect as Boolean) 10Dim i as Integer 11Dim MaxIndex as Integer 12Dim iMsgResult as Integer 13 PWIndex = -1 14 If bDocHasProtectedSheets Then 15 If Not bDoUnprotect Then 16 ' At First query if sheets shall generally be unprotected 17 iMsgResult = Msgbox(sMsgUNPROTECT,36,sMsgDLGTITLE) 18 bDoUnProtect = iMsgResult = 6 19 End If 20 If bDoUnProtect Then 21 MaxIndex = oSheets.Count-1 22 For i = 0 To MaxIndex 23 bDocHasProtectedSheets = Not UnprotectSheet(oSheets(i)) 24 If bDocHasProtectedSheets Then 25 ReprotectSheets() 26 Exit For 27 End If 28 Next i 29 If PWIndex = -1 Then 30 ReDim UnProtectList() as String 31 Else 32 ReDim Preserve UnProtectList(PWIndex) as String 33 End If 34 Else 35 Msgbox (sMsgSHEETSNOPROTECT, 64, sMsgDLGTITLE) 36 End If 37 End If 38 UnProtectSheetsWithPassword = bDocHasProtectedSheets 39End Function 40 41 42Function UnprotectSheet(oListSheet as Object) 43Dim ListSheetName as String 44Dim sStatustext as String 45Dim i as Integer 46Dim bOneSheetIsUnprotected as Boolean 47 i = -1 48 ListSheetName = oListSheet.Name 49 If oListSheet.IsProtected Then 50 oListSheet.Unprotect("") 51 If oListSheet.IsProtected Then 52 ' Sheet is protected by a Password 53 bOneSheetIsUnProtected = UnprotectSheetWithDialog(oListSheet, ListSheetName) 54 UnProtectSheet() = bOneSheetIsUnProtected 55 Else 56 ' The Sheet could be unprotected without a password 57 AddSheettoUnprotectionlist(ListSheetName,"") 58 UnprotectSheet() = True 59 End If 60 Else 61 UnprotectSheet() = True 62 End If 63End Function 64 65 66Function UnprotectSheetWithDialog(oListSheet as Object, ListSheetName as String) as Boolean 67Dim PWIsCorrect as Boolean 68Dim QueryText as String 69 oDocument.CurrentController.SetActiveSheet(oListSheet) 70 QueryText = ReplaceString(sMsgPWPROTECT,"'" & ListSheetName & "'", "%1TableName%1") 71 '"Please insert the password to unprotect the sheet '" & ListSheetName'" 72 Do 73 ExecutePasswordDialog(QueryText) 74 If bCancelProtection Then 75 bCancelProtection = False 76 Msgbox (sMsgSHEETSNOPROTECT, 64, sMsgDLGTITLE) 77 UnprotectSheetWithDialog() = False 78 exit Function 79 End If 80 oListSheet.Unprotect(Password) 81 If oListSheet.IsProtected Then 82 PWIsCorrect = False 83 Msgbox (sMsgWRONGPW, 64, sMsgDLGTITLE) 84 Else 85 ' Sheet could be unprotected 86 AddSheettoUnprotectionlist(ListSheetName,Password) 87 PWIsCorrect = True 88 End If 89 Loop Until PWIsCorrect 90 UnprotectSheetWithDialog() = True 91End Function 92 93 94Sub ExecutePasswordDialog(QueryText as String) 95 With PasswordModel 96 .Title = QueryText 97 .hlnPassword.Label = sMsgPASSWORD 98 .cmdCancel.Label = sMsgCANCEL 99 .cmdHelp.Label = sHELP 100 .cmdGoOn.Label = sMsgOK 101 .cmdGoOn.DefaultButton = True 102 End With 103 DialogPassword.Execute 104End Sub 105 106Sub ReadPassword() 107 Password = PasswordModel.txtPassword.Text 108 DialogPassword.EndExecute 109End Sub 110 111 112Sub RejectPassword() 113 bCancelProtection = True 114 DialogPassword.EndExecute 115End Sub 116 117 118' Reprotects the previousliy protected sheets 119' The passwordinformation is stored in the List 'UnProtectList()' 120Sub ReprotectSheets() 121Dim i as Integer 122Dim oProtectSheet as Object 123Dim ProtectList() as String 124Dim SheetName as String 125Dim SheetPassword as String 126 If PWIndex > -1 Then 127 SetStatusLineText(sStsREPROTECT) 128 For i = 0 To PWIndex 129 ProtectList() = ArrayOutOfString(UnProtectList(i),";") 130 SheetName = ProtectList(0) 131 If Ubound(ProtectList()) > 0 Then 132 SheetPassWord = ProtectList(1) 133 Else 134 SheetPassword = "" 135 End If 136 oProtectSheet = oSheets.GetbyName(SheetName) 137 If Not oProtectSheet.IsProtected Then 138 oProtectSheet.Protect(SheetPassWord) 139 End If 140 Next i 141 SetStatusLineText("") 142 End If 143 PWIndex = -1 144 ReDim UnProtectList() 145End Sub 146 147 148' Add a Sheet to the list of sheets that finally have to be 149' unprotected 150Sub AddSheettoUnprotectionlist(ListSheetName as String, Password as String) 151Dim MaxIndex as Integer 152 MaxIndex = Ubound(UnProtectList()) 153 PWIndex = PWIndex + 1 154 If PWIndex > MaxIndex Then 155 ReDim Preserve UnprotectList(MaxIndex + SBRANGEUBOUND) 156 End If 157 UnprotectList(PWIndex) = ListSheetName & ";" & Password 158End Sub 159 160 161Function CheckSheetProtection(oSheets as Object) as Boolean 162Dim MaxIndex as Integer 163Dim i as Integer 164Dim bProtectedSheets as Boolean 165 bProtectedSheets = False 166 MaxIndex = oSheets.Count-1 167 For i = 0 To MaxIndex 168 bProtectedSheets = oSheets(i).IsProtected 169 If bProtectedSheets Then 170 CheckSheetProtection() = True 171 Exit Function 172 End If 173 Next i 174 CheckSheetProtection() = False 175End Function</script:module>