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 *****
6 Public PWIndex as Integer
9 Function UnprotectSheetsWithPassWord(oSheets as Object, bDoUnProtect as Boolean)
11 Dim MaxIndex as Integer
12 Dim iMsgResult as Integer
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
21 MaxIndex = oSheets.Count-
1
23 bDocHasProtectedSheets = Not UnprotectSheet(oSheets(i))
24 If bDocHasProtectedSheets Then
30 ReDim UnProtectList() as String
32 ReDim Preserve UnProtectList(PWIndex) as String
35 Msgbox (sMsgSHEETSNOPROTECT,
64, sMsgDLGTITLE)
38 UnProtectSheetsWithPassword = bDocHasProtectedSheets
42 Function UnprotectSheet(oListSheet as Object)
43 Dim ListSheetName as String
44 Dim sStatustext as String
46 Dim bOneSheetIsUnprotected as Boolean
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
56 ' The Sheet could be unprotected without a password
57 AddSheettoUnprotectionlist(ListSheetName,
"")
58 UnprotectSheet() = True
61 UnprotectSheet() = True
66 Function UnprotectSheetWithDialog(oListSheet as Object, ListSheetName as String) as Boolean
67 Dim PWIsCorrect as Boolean
68 Dim 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
'"
73 ExecutePasswordDialog(QueryText)
74 If bCancelProtection Then
75 bCancelProtection = False
76 Msgbox (sMsgSHEETSNOPROTECT,
64, sMsgDLGTITLE)
77 UnprotectSheetWithDialog() = False
80 oListSheet.Unprotect(Password)
81 If oListSheet.IsProtected Then
83 Msgbox (sMsgWRONGPW,
64, sMsgDLGTITLE)
85 ' Sheet could be unprotected
86 AddSheettoUnprotectionlist(ListSheetName,Password)
89 Loop Until PWIsCorrect
90 UnprotectSheetWithDialog() = True
94 Sub ExecutePasswordDialog(QueryText as String)
97 .hlnPassword.Label = sMsgPASSWORD
98 .cmdCancel.Label = sMsgCANCEL
99 .cmdHelp.Label = sHELP
100 .cmdGoOn.Label = sMsgOK
101 .cmdGoOn.DefaultButton = True
103 DialogPassword.Execute
107 Password = PasswordModel.txtPassword.Text
108 DialogPassword.EndExecute
113 bCancelProtection = True
114 DialogPassword.EndExecute
118 ' Reprotects the previousliy protected sheets
119 ' The passwordinformation is stored in the List
'UnProtectList()
'
120 Sub ReprotectSheets()
122 Dim oProtectSheet as Object
123 Dim ProtectList() as String
124 Dim SheetName as String
125 Dim SheetPassword as String
126 If PWIndex
> -
1 Then
127 SetStatusLineText(sStsREPROTECT)
129 ProtectList() = ArrayOutOfString(UnProtectList(i),
";
")
130 SheetName = ProtectList(
0)
131 If Ubound(ProtectList())
> 0 Then
132 SheetPassWord = ProtectList(
1)
134 SheetPassword =
""
136 oProtectSheet = oSheets.GetbyName(SheetName)
137 If Not oProtectSheet.IsProtected Then
138 oProtectSheet.Protect(SheetPassWord)
141 SetStatusLineText(
"")
144 ReDim UnProtectList()
148 ' Add a Sheet to the list of sheets that finally have to be
150 Sub AddSheettoUnprotectionlist(ListSheetName as String, Password as String)
151 Dim MaxIndex as Integer
152 MaxIndex = Ubound(UnProtectList())
153 PWIndex = PWIndex +
1
154 If PWIndex
> MaxIndex Then
155 ReDim Preserve UnprotectList(MaxIndex + SBRANGEUBOUND)
157 UnprotectList(PWIndex) = ListSheetName
& ";
" & Password
161 Function CheckSheetProtection(oSheets as Object) as Boolean
162 Dim MaxIndex as Integer
164 Dim 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
174 CheckSheetProtection() = False
175 End Function
</script:module>