Ask Your Question
0

How to protect worksheet using com object

asked 2016-03-14 01:40:44 +0100

pjones1983 gravatar image

I am looking to programmatically set the protect worksheet and set password. If anyone can give me a few pointers.

edit retag flag offensive close merge delete

2 Answers

Sort by » oldest newest most voted
0

answered 2016-03-14 09:36:33 +0100

JohnSUN gravatar image

See subroutines ProtectSheets() and UnprotectSheets() in standard library Tools module Misc.

Methods .Protect(<password>) and .Unprotect(<password>) of object Sheet has one parameter <password> as String.

edit flag offensive delete link more
0

answered 2016-03-15 11:47:53 +0100

pjones1983 gravatar image

updated 2016-03-15 11:57:30 +0100

Works Perfectly. Cheers. John sun

Written In VB.net not complete code

    Dim oSM As Object = CreateObject("com.sun.star.ServiceManager")
    Dim oMM As Object = oSM.createInstance("com.sun.star.frame.Desktop")
    Private WorkSheet As Object
    Dim OpenPar(1) As Object
    OpenPar(0) = MakePropertyValue("Hidden", True)
    Spreadsheet = oMM.LoadComponentFromUrl("file:///'Location'/" & SpreadSheetName, "_blank", 0, OpenPar)
    WorkSheet = Spreadsheet.Sheets.getByName(WorkSheetName)
    WorkSheet.unprotect("password")

       'Do something with worksheet'

     WorkSheet.protect("password")
     Dim arg(-1) As Object
    Spreadsheet.storetoURL("file:///'Location'/" & SaveName, arg)
    Spreadsheet.Close(False)

 Function MakePropertyValue(cName, uValue) As Object
    Dim oPropertyValue As Object
    Dim oSM As Object
    oSM = CreateObject("com.sun.star.ServiceManager")
    oPropertyValue = oSM.Bridge_GetStruct("com.sun.star.beans.PropertyValue")
    oPropertyValue.Name = cName
    oPropertyValue.Value = uValue
    MakePropertyValue = oPropertyValue
End Function
edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2016-03-14 01:40:44 +0100

Seen: 118 times

Last updated: Mar 15 '16