Page 1 of 1

[Solved] Prevent user from saving Calc doc

Posted: Wed Nov 06, 2019 2:01 am
by bartjeman
I want to prevent the user from saving on top of the original file.
Setting to read-only is not an option since the user needs to run my macro which creates a folder on our server and saves the file there.
I need help with the code to stop the Save action, see placeholder " ' some code here to stop the Save"

Code: Select all

' can't make the file Read-Only, otherwise the SaveToServer macro won't run

' this macro runs on event Save, prevents user from Saving over original file

Sub ProtectOriginalFile
Dim oDoc as Object              'prob don't need this
oDoc =  ThisComponent       'prob don't need this

If Filename = "retail.ods" Then
    MsgBox ("Save to Quotes folder first!!", MB_OK + MB_ICONEXCLAMATION,"Warning")
    ' some code here to stop the Save
	Exit Sub
End If
End Sub

Function Filename
Dim url as new com.sun.star.util.URL
Dim srv, bSuccess as Boolean
sURL = thiscomponent.getURL()
srv = createUnoService("com.sun.star.util.URLTransformer")
url.Complete = sURL
srv.parseStrict(url)
Filename = url.Name
End Function

Re: Prevent user from saving calc doc

Posted: Wed Nov 06, 2019 7:51 am
by Zizi64
Tips:
Use the Template file format (.ots). When the user opens a template file, it will lost the URL where it was loaded-in from. It will be appeared as "Untitled1". The user (if he/she not know the file name and URL) can not save the modified document back to the original place accidentally.

Use a prepared Icon (link) for opening the Template. Then the user will not see even the file name... (yes, he/she can edit the link, if he/she WANT anyway to see the URL .)

Re: Prevent user from saving calc doc

Posted: Wed Nov 06, 2019 11:36 am
by JeJe
Useful Macro Information For OpenOffice By Andrew Pitonyak, Listing 4.9: Intercept menu commands, includes Case ".uno:Save" '

Code: Select all

REM Author: Paolo Mantovani
REM Modifed: Andrew Pitonyak
Option Explicit
Global oDispatchInterceptor
Global oSlaveDispatchProvider
Global oMasterDispatchProvider
Global oFrame
Global bDebug As Boolean
'________________________________________________________
Sub RegisterInterceptor
Dim oFrame : oFrame = ThisComponent.currentController.Frame
Dim s$ : s = "com.sun.star.frame.XDispatchProviderInterceptor"
oDispatchInterceptor = CreateUnoListener("ThisFrame_", s)
oFrame.registerDispatchProviderInterceptor(oDispatchInterceptor)
End Sub
'________________________________________________________
Sub ReleaseInterceptor()
On Error Resume Next
oFrame.releaseDispatchProviderInterceptor(oDispatchInterceptor)
End Sub
'________________________________________________________
Function ThisFrame_queryDispatch ( oUrl As Object, _
sTargetFrameName As String, lFlags As Long ) As Variant
Dim oDisp
Dim s$
'the slot protocol causes ooo crash...
If oUrl.protocol = "slot:" Then
Exit Function
End If
If bDebug Then
Print oUrl.complete, sTargetFrameName, lFlags
End If
s = sTargetFrameName
oDisp = oSlaveDispatchProvider.queryDispatch( oUrl, s, lFlags )
'do your management here
Select Case oUrl.complete
Case ".uno:Save" 'disable the save command
Exit Function
'Case "..."
'...
Case Else
' do nothing
End Select
ThisFrame_queryDispatch = oDisp
End Function
'________________________________________________________
Function ThisFrame_queryDispatches ( mDispArray ) As Variant
ThisFrame_queryDispatches = mDispArray
End Function
'________________________________________________________
Function ThisFrame_getSlaveDispatchProvider ( ) As Variant
ThisFrame_getSlaveDispatchProvider = oSlaveDispatchProvider
End Function
'________________________________________________________
Sub ThisFrame_setSlaveDispatchProvider ( oSDP )
oSlaveDispatchProvider = oSDP
End Sub
'________________________________________________________
Function ThisFrame_getMasterDispatchProvider ( ) As Variant
ThisFrame_getMasterDispatchProvider = oMasterDispatchProvider
End Function
'________________________________________________________
Sub ThisFrame_setMasterDispatchProvider ( oMDP )
oMasterDispatchProvider = oMDP
End Sub
'________________________________________________________

Sub ToggleDebug()
'be carefull! you will have a debug message
' for each dispatch....
bDebug = Not bDebug
End Sub


[Solved] Prevent user from saving calc doc

Posted: Wed Nov 06, 2019 6:14 pm
by bartjeman
Thank you Zizi64 and JeJe

Of course... template. We get so caught up in the problem we forget the elegant solution

Re: [Solved] Prevent user from saving Calc doc

Posted: Thu Nov 07, 2019 12:03 am
by musikai
You can also just disable the Save Button by pretending the file hasn't changed:

Code: Select all

ThisComponent.setModified(False)
Of course this still allows using "Save as".

Re: [Solved] Prevent user from saving Calc doc

Posted: Thu Nov 07, 2019 8:25 am
by Zizi64
You can also just disable the Save Button by pretending the file hasn't changed:

Code: Select all Expand view
ThisComponent.setModified(False)
Note:
The Save function (the toolbar button and the menu item) is active in the newer versions of the LibreOffice even if the document has not changed.

Re: [Solved] Prevent user from saving Calc doc

Posted: Thu Nov 07, 2019 11:28 am
by musikai
Zizi64 wrote:
You can also just disable the Save Button by pretending the file hasn't changed:

Code: Select all Expand view
ThisComponent.setModified(False)
Note:
The Save function (the toolbar button and the menu item) is active in the newer versions of the LibreOffice even if the document has not changed.
oh, you're right. Thank you! Fortunately the OP can make usage of the template solution.