How to append date variable in macro to export to .pdf file?

In LO calc, I have a simple macro to export a single sheet to a .pdf file. This export line works:

oDoc.storeToURL(“file:///home/username/Desktop/Spreadsheets/test.pdf”,
aMediaDescriptor())

But I need to append the current simple date to the filename, trying this:

oDoc.storeToURL(“file:///home/monty/Desktop/Spreadsheets/test’$(date
+%Y-%m-%d)’.pdf”, aMediaDescriptor())

That saves the filename literally as:

test’$(date +%Y-%m-%d)’.pdf

I have tried various combinations of parenthesis and quotes but am missing something!

Thanks!

Many things are easier in Python-UNO. This saves as test2018-01-03.pdf.

import datetime
today = datetime.date.today()
filename = "file:///home/monty/Desktop/Spreadsheets/test%s.pdf" % today
oDoc = XSCRIPTCONTEXT.getDocument()
oDoc.storeToURL(filename, aMediaDescriptor)

For more details, see my answer at Default PDF Filename - #2 by jimk.

There are also a number of solutions for Basic given at [Solved] Format function and dates (View topic) • Apache OpenOffice Community Forum. It looks like the best way is to use service com.sun.star.util.NumberFormatter.

EDIT:

Here is Basic code.

sDatestamp = Format(Now(),"YYYY-MM-DD")
filename = "file:///home/monty/Desktop/Spreadsheets/test" & sDatestamp & ".pdf"
oDoc = ThisComponent
oDoc.storeToURL(filename, aMediaDescriptor)

Based on @mikekaganski’s comment, it seems the link I posted from the OOo forum is partly out of date. “MMMM” now works properly.

1 Like

Re: “It looks like the best way is to use service com.sun.star.util.NumberFormatter”: this shows that Format function can easily be used in Basic.