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