[Solved] Prevent user from saving Calc doc

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
User avatar
bartjeman
Posts: 177
Joined: Sun Jan 03, 2010 6:23 am
Location: Toronto

[Solved] Prevent user from saving Calc doc

Post 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
Last edited by bartjeman on Wed Nov 06, 2019 6:17 pm, edited 1 time in total.
OpenOffice 4.1.7 on Windows 10
User avatar
Zizi64
Volunteer
Posts: 11359
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Prevent user from saving calc doc

Post 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 .)
Last edited by Zizi64 on Wed Nov 06, 2019 12:31 pm, edited 1 time in total.
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
JeJe
Volunteer
Posts: 2780
Joined: Wed Mar 09, 2016 2:40 pm

Re: Prevent user from saving calc doc

Post 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

Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
User avatar
bartjeman
Posts: 177
Joined: Sun Jan 03, 2010 6:23 am
Location: Toronto

[Solved] Prevent user from saving calc doc

Post by bartjeman »

Thank you Zizi64 and JeJe

Of course... template. We get so caught up in the problem we forget the elegant solution
OpenOffice 4.1.7 on Windows 10
musikai
Volunteer
Posts: 294
Joined: Wed Nov 11, 2015 12:19 am

Re: [Solved] Prevent user from saving Calc doc

Post 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".
Win7 Pro, Lubuntu 15.10, LO 4.4.7, OO 4.1.3
Free Project: LibreOffice Songbook Architect (LOSA)
http://struckkai.blogspot.de/2015/04/li ... itect.html
User avatar
Zizi64
Volunteer
Posts: 11359
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: [Solved] Prevent user from saving Calc doc

Post 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.
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
musikai
Volunteer
Posts: 294
Joined: Wed Nov 11, 2015 12:19 am

Re: [Solved] Prevent user from saving Calc doc

Post 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.
Win7 Pro, Lubuntu 15.10, LO 4.4.7, OO 4.1.3
Free Project: LibreOffice Songbook Architect (LOSA)
http://struckkai.blogspot.de/2015/04/li ... itect.html
Post Reply