[Solved] Insert LibreOffice storage path with macro

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
sp-it
Posts: 2
Joined: Mon Jan 02, 2023 12:43 pm

[Solved] Insert LibreOffice storage path with macro

Post by sp-it »

Hello,
we use a Macro for Libre Office which lets the user choose a folder in which the file should be saved. It also sets the working path.

Since LIbre Office 7.3, the working path is no longer used when opening Save As, but the entry in the expert configuration:

Code: Select all

org.openoffice.Office.Common:LastDirectory['WriterSaveAs'].
Even if I save once in a folder with our macro and want to save the next file in another folder, the old folder is still displayed in the save dialog.

This is how we open the save dialog at the moment:

Code: Select all

oDispatchHelper.executeDispatch( document, ".uno:Save", "", 0, Array() )
Does anyone know of a way to pass the save path to the save dialog using a macro?

I thank you in advance for any help.
Kind regards, Maurice
Last edited by Hagar Delest on Thu Jan 05, 2023 12:55 pm, edited 1 time in total.
Reason: tagged solved.
Libre Office 7.3 on Ubuntu 18.04
JeJe
Volunteer
Posts: 2779
Joined: Wed Mar 09, 2016 2:40 pm

Re: Insert Libre Office storage path with macro

Post by JeJe »

You can set the save as directory as an argument when you create or open the document. If you run this then try saving the new document from the Save as menu item.

Code: Select all

Dim mArgs(1) as New com.sun.star.beans.PropertyValue
pths= CreateUnoService("com.sun.star.util.PathSettings")

mArgs(0).name = "SuggestedSaveAsDir"
mArgs(0).value=pths.getpropertyvalue("Work_writable")
margs(1).name = "SuggestedSaveAsName"
margs(1).value = "SuggestedName"

oDoc=StarDesktop.loadComponentFromURL("private:factory/swriter","_blank",0,margs())

Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
User avatar
Zizi64
Volunteer
Posts: 11359
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Insert Libre Office storage path with macro

Post by Zizi64 »

his is how we open the save dialog at the moment:

Code: Select all

oDispatchHelper.executeDispatch( document, ".uno:Save", "", 0, Array() )

Does anyone know of a way to pass the save path to the save dialog using a macro?
It is possible but you must use the API functions instead of the Dispatcher. (FilePicker, FolderPicker)
And you can enforce the dialogs of the LibreOffice even if it is set to System Dialogs in the settings.

https://extensions.libreoffice.org/hu/e ... s/show/778
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.
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Insert Libre Office storage path with macro

Post by Villeroy »

Code: Select all

Sub test_WriterSaveAs_LastPath()
'calls getSetupNode
'GlobalScope.BasicLibraries.loadLibrary("MRILib")
sNode = "/org.openoffice.Office.Common"
oNode = getSetupNode(sNode)
'mri oNode
sValue = oNode.getByHierarchicalName("Misc/FilePickerLastDirectory/WriterSaveAs/LastPath")
print sValue
End Sub

Function getSetupNode(sNodePath$)
Dim aConfigProvider, args(0) As new com.sun.star.beans.PropertyValue
	aConfigProvider = createUnoService("com.sun.star.configuration.ConfigurationProvider")
	args(0).Name = "nodepath"
	args(0).Value = sNodePath
	getSetupNode = aConfigProvider.createInstanceWithArguments("com.sun.star.configuration.ConfigurationAccess", args())
End Function
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
sp-it
Posts: 2
Joined: Mon Jan 02, 2023 12:43 pm

Re: Insert LibreOffice storage path with macro

Post by sp-it »

Hello,
thank you for all the answers.
We solved our problem with the answer of JeJe but we also have tried the API approach but we run into a problem.
We already tried this code:

Code: Select all

		Dim FPType(0) As Integer
		FPType(0) = 4
		
		Dim oFilePicker As Object, FileName As String
		FileName = ""
		'FilePicker initialization
		oFilePicker = CreateUnoService("com.sun.star.ui.dialogs.FilePicker")
		oFilePicker.DisplayDirectory = ConvertToURL("Path/to/folder/")
		oFilePicker.AppendFilter("Writer Documents",  "*.odt")
		FilePicker.CurrentFilter = "Writer Documents"
		oFilePicker.Title = "Save a Writer document"
				'execution and return check (OK?)
		oFilePicker.initialize(FPType())
		If oFilePicker.execute = com.sun.star.ui.dialogs.ExecutableDialogResults.OK Then
		FileName = oFilePicker.Files(0)
		thisComponent.storeAsURL(FileName, array())
		End If
The problem with our code is that the document will be saved but the .odt at the end is missing.
It also does not give the title or the filter to the save window.
Is there an example code to save a file with the API?

Thanks again for your answers and looking forward to hear from you again.
Libre Office 7.3 on Ubuntu 18.04
JeJe
Volunteer
Posts: 2779
Joined: Wed Mar 09, 2016 2:40 pm

Re: Insert LibreOffice storage path with macro

Post by JeJe »

If you tick

Tools menu/options/General tab/Use LibreOffice dialogue boxes

then you have more control over the native dialogue that's shown - but its old fashioned looking.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
JeJe
Volunteer
Posts: 2779
Joined: Wed Mar 09, 2016 2:40 pm

Re: [Solved] Insert LibreOffice storage path with macro

Post by JeJe »

Someone else had a similar query - don't know whether their solution might be good for you...

https://forum.openoffice.org/en/forum/v ... 489f220881
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Post Reply