Set default folder location for Save As and Save a Copy

Can I set a default folder to show when the Save As or Save a Copy dialog box opens?
It currently defaults to the working directory of the open file. I want to change it to open else where.

I can trigger the Save option - assigning a macro to Tools > Customise > Events > Save Document, and this works.
However, nothing seems to work when I assign it to Tools > Customise > Events > Save Document AS

I’m working in Windows 10 environment

If you are already agreeing to a macro, perhaps just create a macro to accomplish the entire save? If the Save As path is a regular thing, then you could use something like this harvested by Gemini–and checked:

Sub SaveAsExample

    Dim oDoc As Object
    Dim sFilePath As String
    Dim oArgs(0) As New com.sun.star.beans.PropertyValue

    ' Get the current document
    oDoc = ThisComponent

    ' Define the desired file path and name
    ' IMPORTANT: Use 'file:///' for local paths.
    ' Adjust the path and filename as needed.
    sFilePath = "file:///C:/Users/Admin/Documents/MySavedCalcDocument.ods" ' Example path for Windows
    ' For Linux/macOS, it might look like: "file:///home/YourUser/Documents/MySavedCalcDocument.ods"

    ' Set the file type (optional, but good practice)
    ' This example saves as an ODS (OpenDocument Spreadsheet)
    ' You can find other filter names in LibreOffice documentation or by
    ' inspecting the "Save As" dialog's filter options.
    oArgs(0).Name = "FilterName"
    'oArgs(0).Value = "Calc MS Excel 2007 XML" ' Example for .xlsx
    oArgs(0).Value = "calc8"

    ' Perform the Save As operation
    oDoc.storeAsURL sFilePath, oArgs()

End Sub

Note that if you change the file type for the filter, you still have to change the extension to .xlsx or whatever. I’m not sure that in this scenario setting the filter is even necessary, but I do a lot of CSV saves, and there it is.

I already have the code to initiate the default folder and file name to save to. The problem is if the user clicks the File menu > Save As or Save a Copy I cannot get it to run my macro.
I thought I could use the Events under Tools > Customise, but when I assign the macro to “Save Document As” or “Storing or exporting copy of document”, it doesn’t use the file path in the macro I’ve assigned. Which is virtually identical to want you have provided.

On further investigation it’s probably a bug, because my macro does run when assigned to Events under Tools > Customise, i.e. “Save Document As” or “Storing or exporting copy of document”, but only after the system Save As dialog has opened.

Therefore it seems to open the Save As and (Copy) dialog regardless, then it will run an assigned macro. Therefore I can’t set a default folder location to open for Save As or Save copy

The macro for the OnSaveAs event is called before saving the file with the name previously selected in the dialog, the macro for the OnSaveAsDone event is called after saving.
Note the setArgs method with the property parameter SuggestedSaveAsDir (since LO 6.3).

1 Like

If I assign a simple message macro like below

Sub SaveAsMessage

	Msgbox "Please use the save button on the sheet!"
	
End Sub

to either of these highlighted events below. It doesn’t show the message box until I close the Save As dialog box. I’m using LO 25.2. How do I get the message box to show BEFORE the Save As dialog box appears. This will then hopefully enable me to assign a default folder for the Save As dialog box
image

Please note I’ve only been using LO for just over a week now, so am still finding my way around.

As I wrote above, macros for OnSaveAs (Save Document As) and OnSaveAsDone (Document has been saved as) events are executed after the dialog for selecting a folder and a saved name.
Alternative: after opening the file, you can execute a macro like this:

Sub TestSuggestedSaveAsDir()
  Dim props(0) As New com.sun.star.beans.PropertyValue
  props(0).Name="SuggestedSaveAsDir"
  props(0).Value=ConvertToURL("C:\temp\MyFiles")
  ThisComponent.setArgs props
End Sub

Now when trying to “Save file as” the C:\temp\MyFiles folder will be specified.

1 Like

Of course setup the default directory when opening the file!
That’s perfect. Thank you. Works a treat.

1 Like

I can say the same about myself after 275 weeks of using LO. :slight_smile: