[Solved] Prevent user from saving Calc doc

Creating a macro - Writing a Script - Using the API

[Solved] Prevent user from saving Calc doc

Postby bartjeman » Wed Nov 06, 2019 2:01 am

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   Expand viewCollapse view
' 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.0.1 on Windows 7
User avatar
bartjeman
 
Posts: 146
Joined: Sun Jan 03, 2010 6:23 am
Location: Toronto

Re: Prevent user from saving calc doc

Postby Zizi64 » Wed Nov 06, 2019 7:51 am

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; LO6.1.6 on Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-6.3.3; AOO4.1.6
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.
User avatar
Zizi64
Volunteer
 
Posts: 8517
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Prevent user from saving calc doc

Postby JeJe » Wed Nov 06, 2019 11:36 am

Useful Macro Information For OpenOffice By Andrew Pitonyak, Listing 4.9: Intercept menu commands, includes Case ".uno:Save" '

Code: Select all   Expand viewCollapse view
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

Openoffice 4.1.2
Windows 8
JeJe
Volunteer
 
Posts: 624
Joined: Wed Mar 09, 2016 2:40 pm

[Solved] Prevent user from saving calc doc

Postby bartjeman » Wed Nov 06, 2019 6:14 pm

Thank you Zizi64 and JeJe

Of course... template. We get so caught up in the problem we forget the elegant solution
OpenOffice 4.0.1 on Windows 7
User avatar
bartjeman
 
Posts: 146
Joined: Sun Jan 03, 2010 6:23 am
Location: Toronto

Re: [Solved] Prevent user from saving Calc doc

Postby musikai » Thu Nov 07, 2019 12:03 am

You can also just disable the Save Button by pretending the file hasn't changed:
Code: Select all   Expand viewCollapse view
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/libreofficesongbookarchitect.html
musikai
Volunteer
 
Posts: 254
Joined: Wed Nov 11, 2015 12:19 am

Re: [Solved] Prevent user from saving Calc doc

Postby Zizi64 » Thu Nov 07, 2019 8:25 am

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; LO6.1.6 on Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-6.3.3; AOO4.1.6
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.
User avatar
Zizi64
Volunteer
 
Posts: 8517
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: [Solved] Prevent user from saving Calc doc

Postby musikai » Thu Nov 07, 2019 11:28 am

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/libreofficesongbookarchitect.html
musikai
Volunteer
 
Posts: 254
Joined: Wed Nov 11, 2015 12:19 am


Return to Macros and UNO API

Who is online

Users browsing this forum: RPG and 9 guests