Saving current document with password from macro

I’m creating “.xlsx” documents which are sent to customers, each document needs to be password protected. I want to run a macro which can save this document with a password.

I’ve tried using this solution, but it just does nothing after the dialog runs.

(I am very new to LibreOffice, so if any info is missing do let me know)

Welcome!

Excuse me, what code are you talking about now? About the code from the text of the question or about some line from the text of the answer?
The fact is that the lines of code given in the answer should not cause any dialog windows.

I should’ve made it clearer, I tried the code in the question and in the answer.
Both didn’t do anything

It protects the current sheet as in menu:Tools>Protect Sheet…

Thanks for the suggestion. However, even if I do that, nothing happens, no new file is created, and the spreadsheet itself isn’t protected

There is a macro shipped with the office suite. You find it under Tools>Macros>Organize>Basic…
Container “LibreOffice Macros”
Library Tools
Module Misc
Sub ProtectSheets and UnprotectSheets

This is a completely different task.

I see what you mean now, I don’t (exactly) want the cells themselves being protected against modification.
I want the entire file to be password protected, as if you saved with a password

I just need a VB script which can password protect a spreadsheet.
Specifically a script which can be later called by a separate program (Not libreOffice)

Use an external encription program such as pgp.

That’s what I did initially, the problem is that LibreOffice cannot parse pgp-encrypted documents properly.
And I don’t think the customers would appreciate me telling them to use the terminal to decrypt their spreadsheets

Pgp decrypts all of your pgp-encrypted files. One program to encrypt and decrypt your files properly regardless of file types.

Right, but the customer already has a spreadsheet program which can decrypt files.
I’m just not in a position to tell the customers to install new software just to decrypt their data, which we create and encrypt

You don’t have to use that feature. Better use pgp.

We were very close to a solution, but I just discovered a fairly serious problem.
This “solution” only works for LibreOffice, if I try to open it from Microsoft Word, or Word-online, it just doesn’t work.
I also tried this suggestion, it creates the file without password, no matter what.
Any help is very appreciated, I’m out of options

Why should MS run our code? They hardly support our document format.
Simply use an encryption program for encryption and use your office suite to generate office documents in the native file format of the program you are using. Generating MS Office documents with LibreOffice makes no sense.

I’ll try to explain the problem in more detail.
We’re generating “.xlsx” documents from C++ using a library called QXlsx.
The problem is, is that library doesn’t allow saving documents with passwords, I tried using Xlnt, but their encryption scheme isn’t supported by LibreOffice or MS office, meaning that neither of those program can decode the document after encrypting.
The only way I found to encrypt a document from C++ is to use this macro in LibreOffice, and call soffice with the “macro” arguments .
Now the problem is, is that macro does work, it generates encrypted documents, but only LibreOffice can decode them. I tried MS office, and word-online, this is where I am at right now

This means that your problem has absolutely nothing to do with LibreOffice.

Well, I am generating the encrypted documents through LibreOffice’s macros.
Which only LibreOffice can open(the documents), it might not be at fault, maybe my script is incorrect.
That’s the I need help with

Stroring to encrypted files by unexperienced users is a frequent cause of data loss. No remedy then!

If you feel sure to know what you are doing, you can use the code below to start. (LibreOffice Basic isn’t “VBA”.)

Sub saveEncrypted(Optional pPwd As String)
If IsMissing(pPwd) Then pPwd = "encrypted"
doc = ThisComponent
cURL = doc.URL
If (cURL="") OR NOT (FileExists(cURL)) Then
 Print "A complete existing URL is needed in the example."
 REM Of course, the URL to store to can be any URL the calling
 REM user has the right to write to. 
Exit Sub
End If
Dim saveEncryptedArgs(0) As New com.sun.star.beans.PropertyValue
saveEncryptedArgs(0).Name  = "Password"
saveEncryptedArgs(0).Value = pPwd
doc.storeAsURL(cURL, saveEncryptedArgs)
doc.close(True)
End Sub

My extra problem is to understand for what reason this should be done by a “macro”.

This seems promising, one question.
If I save this file somewhere on disk, can I use LibreOffice’s CIL to run this script with arguments?