Macro to protect spreadsheet without UI popup

Hi.

I am trying to protect a Calc sheet while specifying some attributes, like allowing to select non-protected cell and disallowing the selection of cells that are protected.

These attributes are available via the user interface by right-clicking the sheet name and selecting those options. However, I would like to trigger those options in a macro without having the UI popup.

How could this be done?

I have attached a spreadsheet with an embedded macro, but it does not work.

Test_Protect_Sheet.ods (13.9 KB)

Any suggestions?

Thanks

JF

See container “Application Macros”, library “Tools”, module “Misc”, routine “ProtectSheets”

Thanks for your reply, Villeroy.

I was able to load the “Misc” module from the “Tools” library using the following command: GlobalScope.BasicLibraries.LoadLibrary(“Tools”).

I was also able to test it using the following code:
oData = GetRegistryKeyContent(“org.openoffice.UserProfile/Data”, True)
MsgBox "oData.givenname: " & oData.givenname & " and oData.sn: " & oData.sn
Msgbox "Product name: " & GetProductname

However, calling:
Sub ProtectSheets(Optional oSheets as Object)
Dim i as Integer
Dim oDocSheets as Object
If IsMissing(oSheets) Then
oDocSheets = StarDesktop.CurrentFrame.Controller.Model.Sheets
Else
Set oDocSheets = oSheets
End If

For i = 0 To oDocSheets.Count-1
	oDocSheets(i).Protect("")
Next i

End Sub

return the message “Basic runtime error: Property or method not found: Sheets.” in reference to the statement: “StarDesktop.CurrentFrame.Controller.Model.Sheets”

Even if I could fix this Basic runtime error, I still don’t know how to pass the specific arguments to set the way I would like the sheet to be protected (for example, “Select unprotected cells” only). Are those arguments documented somewhere? How do I pass it to the “ProtectSheets” Subroutine?

Thanks again for your help on this.

I am still searching for a solution but I was able to protect a sheet with a password and without a user prompt using the fowling code:

Dim oSheet As Object
Dim sPass As String
sPass = “”
oSheet = ThisComponent.CurrentController.getActiveSheet
oSheet.protect(sPass)

However, I cannot figure out to set the different options when protecting a sheet.

I’ve tried to set the following attributes based on some info that I found:
oSheet.getPropertySetInfo(“AllowSelectUnprotectedCells”, False)
oSheet.getPropertySetInfo(“AllowSelectProtectedCells”, False)
oSheet.getPropertySetInfo(“AllowInsertRows”, False)
oSheet.getPropertySetInfo(“AllowDeleteRows”, False)
oSheet.getPropertySetInfo(“AllowInsertColumns”, False)
oSheet.getPropertySetInfo(“AllowDeleteColumns”, False)

I don’t get a macro error, but it does not do anything, and the protections options default to what ever was set prior to protecting the sheet.

Any idea how to set the options available via the UI when protecting a sheet?

Thanks again for your help.

There is no API for that. You only can unprotect the sheet, and then re-protect it again in the same session, keeping the same options (implemented in commit cc17915f6937a333bf7b741110dbdee39b1e3eb4).

If you generate a Calc document yourself, you can copy a pre-prepared sheet with the required sheet protection properties.
Also (with a lot of code) you can create a sheet on the fly with the required sheet protection properties (without using technology AccessibleContext :slight_smile: ).

Thanks Mike, much appreciated. At least with your enhancement, I can now protect the sheet with the previous options without the UI popping up.

JFG