Macro to unprotect a range

The macro code below is meant to:

  • select the sheet DATA1 in the current document
  • unprotect the sheet with the password 1234
  • turn off cell protection for the range T7:T4326
  • protect the sheet with password 1234

However, after running, no cells in the specified range are unprotected.
Where am I going wrong?
Thank you in advance.

Sub UnprotectRange
    Dim oDoc As Object
    Dim oSheet As Object
    Dim oRange As Object

    oDoc = ThisComponent
    oSheet = oDoc.getSheets().getByName("DATA1")
    oSheet.unprotect("1234")

    oRange = oSheet.getCellRangeByName("T7:T4326")
    oRange.CellProtection.IsLocked = False

    oSheet.protect("1234")
End Sub
Dim oPC As New "com.sun.star.util.CellProtection"

With oPC
	.IsLocked = False
	.IsHidden = False
End With
oRange.CellProtection = oPC

One generalising remark:
For structured properties you can’t directly change an internal property. You need to either

  • create a new instance, assign values as wanted, and assign the structure to the original object. This was already demonstrated.
  • get a copy of the original structure to a variable, make the wanted changes/assignments, and write the changed structure back to the source object.


And an unsolicited suggestion:
Use cell styles in such a case. For more details see the attached example. (There are pürobably also some disadvantages/surprises. One of them may be the need to create an unprotected style as the hierarchical root for what youz actually want.)
bMacrosHandleCellProtection.ods (11.9 KB)