We will be migrating from Ask to Discourse on the first week of August, read the details here

Ask Your Question
0

BASIC+Calc: how to check assigned password

asked 2021-05-12 04:23:31 +0200

lonk gravatar image

updated 2021-05-12 05:49:34 +0200

karolus gravatar image

How can I check the password of this active sheet ? I think it is impossible for it is the security reason.

Now I use:

REM  *****  BASIC  *****
Option Explicit
Global sPassword As String

Sub LockActiveSheet
    Dim oSheet As Object : oSheet = ThisComponent.CurrentController.ActiveSheet
    sPassword = "1234"
'   Lock active sheet   '
    oSheet.Protect(sPassword)
End Sub

Sub CheckCorrectPassword
    Dim oSheet As Object : oSheet = ThisComponent.CurrentController.ActiveSheet
    oSheet.UnProtect(sPassword)
    oSheet.getCellByPosition(0, 0).setString("X")
    If  oSheet.getCellByPosition(0, 0).String = "X" Then
        MsgBox "Password is correct.", , ""
        oSheet.Protect(sPassword)       
    Else
        MsgBox "Password is not correct.", , ""
    End If
End Sub

In the example, I have to interfere a cell in the active sheet.

Are there any ways to make it shorter and without touching even a cell ?

edit retag flag offensive close merge delete

Comments

1

Code edited for readability

karolus gravatar imagekarolus ( 2021-05-12 05:49:41 +0200 )edit

1 Answer

Sort by » oldest newest most voted
1

answered 2021-05-12 15:15:26 +0200

erAck gravatar image

You could ask boolean oSheet.isProtected(). However, calling unprotect() with a wrong password will already throw lang::IllegalArgumentException that results in a BASIC runtime error. So usual BASIC error handling is applicable, like

On Error Goto ErrorHandler
oSheet.unprotect(sPassword)
print oSheet.isProtected()
REM do something good
Exit Sub
ErrorHandler:
MsgBox "Password is not correct.", , ""
edit flag offensive delete link more

Comments

Dear @erAck,

Thank you so much, hereunder code is just a memo.

Sub CheckCorrectPassword
    Dim oSheet As Object : oSheet = ThisComponent.CurrentController.ActiveSheet
    oSheet.unprotect(sPassword)
    If  oSheet.isProtected() = False Then   
        MsgBox "Password is correct.", , ""
        oSheet.protect(sPassword)
    Else
        MsgBox "Password is not correct.", , ""
    End If
End Sub
lonk gravatar imagelonk ( 2021-05-13 01:13:13 +0200 )edit

It will not work if unprotect() fails due to a wrong password. Read what I wrote about it throwing an exception resulting in a BASIC error. The following isProtected() isn't even executed in that case.

erAck gravatar imageerAck ( 2021-05-13 20:08:05 +0200 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2021-05-12 04:23:31 +0200

Seen: 22 times

Last updated: May 12