Saving current document with password from macro

We are creating spreadsheets with the “.xlsx” file extension using C++, LibreOffice macro API is used to encrypt them (Unless something else is found).
When I say MS-word, I mean the LibreOffice equivalent for Microsoft/Windows. That’s the program our customers use to open the spreadsheet we send them.

Apologies if something’s unclear I’m fairly new to this whole spreadsheet thing

MS Word is an analog of LibreOffice Writer. For LibreOffice Calc, there is MS Excel. And the whole LibreOffice suite’s MS analog is called MS Office.

1 Like

Thank you so much, this worked perfectly. We’re even able to decrypt it from gmail.

For some reason every time I specified the “FilterName” as “Calc MS Excel 2007 XML” it didn’t encrypted the file.

This is the script right now

sub SaveWithPassword(inputFile as String, outputFile as String, password as String) as Integer
	
	' Convert file paths to a format LibreOffice can undersntad
	dim inputFileURL as string
	inputFileURL = ConvertToURL(inputFile)
	
	dim outputFileURL as String
	outputFileURL = ConvertToURL(outputFile)

	' Properties used to load the component
	dim loadComponentProperties(0) As New com.sun.star.beans.PropertyValue

	' Don't launch a new window when loading the document
	loadComponentProperties(0).Name = "Hidden"
	loadComponentProperties(0).Value = True


' If an error occured, jump to handleError label
on error goto handleError

	' Load the document
	dim doc as Object 
	doc = stardesktop.LoadComponentFromURL(inputFileURL, "_blank", 0, loadComponentProperties)

	
	' Output document encryption data
	Dim encryptionData(1) As New com.sun.star.beans.NamedValue

    encryptionData(0).Name = "CryptoType"
    encryptionData(0).Value = "Standard"
    encryptionData(1).Name = "OOXPassword"
    encryptionData(1).Value = password


	' Properties used when saving the document
	dim saveEncryptedArgs(1) As New com.sun.star.beans.PropertyValue


    saveEncryptedArgs(0).Name = "FilterName"
    saveEncryptedArgs(0).Value = "Calc MS Excel 2007 XML"
    
    saveEncryptedArgs(1).Name = "EncryptionData"
    saveEncryptedArgs(1).Value = encryptionData
	
	' Store this docuemnt with a password where specified.
	' Using storeAsURL might be preferred in our case, since we want to overwrite the documents
	' doc.storeToURL(outputFileURL, saveEncryptedArgs)
	doc.storeAsURL(outputFileURL, saveEncryptedArgs)
	
	' Close the document
	doc.close(True)
	
	' Return '1' (true) if succesfull
	 SaveWithPassword = 1
	
	exit sub
	
handleError:

	' Return '0' (false) if an error occured
 	SaveWithPasswor2 = 0

end sub

I may have just one last question. Is there a way to return a value from the script?
Because when I run the script from C++ it always returns 0 (I have other ways to verify that the script worked).

This clarifying question is not relevant to the original discussion

How do you call it?
Also, don’t forget about this syntax features:
image

Note the typo. There are possibly more - e.g., it’s unclear what “I have other ways to verify that the script worked” might mean, unless there’s a missing “no”, like “I have no other ways …” - or do you mean that you can workaround even if there’s no way to return a value?

A “script” term is used without clarification: does “script” mean the Basic sub (which, being sub, can’t return anything, as @JohnSUN mentioned)? Or is that a shell script - so you expect the soffice process to return an exit code? So that boils down to: do you use UNO API to control soffice from C++ (and so you are able to call e.g. XScript::invoke), or are you trying to use command line interface?

I have figured out a way to work around the limitation of the script returning only ‘0’ to verify that the script ran successfully. But, I’d prefer if I could actually return more specific results from the script itself.

When I say script, I mean a LibreOffice macro which can be run by a C++ program, atm I’m (very unfortunately) using system() to invoke the soffice. XScript::invoke seems very interesting, I’ll give it a try later