Set filename depending on a variable

I use LO Base for invoices, I input the data, and it returns me an ODT file, so I export it to PDF, and then I rename the file, so it fits the invoice ID. I want to automate this process as much as possible and, since I can’t directly export it to PDF (AFAIK), I’m focusing now on trying to make LO to automatically export the ODT with the name of the variable InvoiceID. I’ve been looking around, but all I find are posts about how to use variables or using the name of the file inside the document, but nothing else.

Hello,
Take a look at the sample in this post → How to print a base report and send it by mail (mailmerge)
.
It uses output from Report Builder, names each invoice, saves as odt, converts to PDF and then emails it. Depending on your specifics you should be able to modify it to your needs.

Hi! Thanks for replying, so I guess I’d had to create a macro using ReportPDFstore. I don’t know about coding macros, so I’m not sure if I’ll do it right but I guess I’ll have to create a macro with this?:

Sub ReportPDFstore(oReport As Object)
    Dim stUrl As String
    Dim arg(0) As New com.sun.star.beans.PropertyValue
    arg(0).name = "FilterName"
    arg(0).value = "writer_pdf_Export"
    stUrl = "file:///...."
    oReport.storeToURL(stUrl, arg())
End Sub

maybe something like this?

Sub ReportPDFstore(oReport As Object)
    Dim stUrl As String
    Dim arg(0) As New com.sun.star.beans.PropertyValue
    arg(0).name = [InvoiceID]
    arg(0).value = "writer_pdf_Export"
    stUrl = ./Invoices/
    oReport.storeToURL(stUrl, arg())
End Sub

Just modified the second sub in the sample to be:

Sub mmReportPDFstore(oReport AS OBJECT, sCust As String, sEmail As String)
Rem LO Base documentation - Chapter 9
    DIM stUrl       AS STRING
    DIM sInvoice AS STRING
    DIM arg(0)     AS NEW com.sun.star.beans.PropertyValue
    arg(0).name = "FilterName"
    arg(0).value = "writer_pdf_Export"
Rem Modify file name to include identifying item and curent date
    sInvoice = "Invoice_" & sCust & "_" & FORMAT(Now, "YYYYMMDD") & ".pdf"
Rem Adjust for your system
    stUrl = "file:///home/MY_DIRECTORY/Documents/" & sInvoice
    oReport.storeToURL(stUrl, arg())
Rem Comment this next line if you do not wish to send email's.
'    mmSimpleMailWithPDF(sInvoice, sEmail)
End Sub

and created PDF’s without issue.
Ubuntu 20
LO v7.3.4.2

Thanks, I’ll check it out! It came to my attention that you used sEmail, may I ask why?

As stated, just modified the second macro to demo the use of PDF. Did not re-write for your insertion as I do not know your precise needs or use. In that case more is likely to be changed.
.
I did not want to completely break my original code. Wanted that intact so just quickly modified for demo purposes.

@Moshpirit
.
Found a sample you had posted in past. Modified the SQL so the report originates from a form - ReportFilter. The invoice to be printed is selected from a list box. Select then push button.
.
But before testing, the file location in the macro must be changed to your needs. It will not work until correctly changed.
.
Example of invoices for askLO.odb (43.3 KB)
.
Also do not be concerned if you choose not to use as it is involving macros. Just offered as something going straight to PDF. :slight_smile:

Thank you, that’s very kind of you! :blush:

I imagine that the macro is Example of invoices for askLO.odb/Standard/Module1. It gives me an error when running it:

BASIC runtime error.
Argument is not optional

It returns me to line 27: sInvoice = "Invoice_" & sCust & "_" & FORMAT(Now, "YYYYMMDD") & ".pdf"

It appears you are trying to execute the code directly. In my comment it states:

Use the form to generate the PDF. The push button executes the routine mmReportStart which call routine mmReportPDFstore. But again, in this second routine you must first change the file location for your system.

Oh, you’re right! The directory is set, but I went to the report, pressed “Store as PDF” and opened the macro with this error:

BASIC runtime error.
An exception occurred 
Type: com.sun.star.io.IOException
Message: SfxBaseModel::impl_store </home/alberto/Documentos/Invoices/Invoice_2122002_20220726.pdf> failed: 0x81a(Error Area:Io Class:Parameter Code:26) at /home/buildslave/source/libo-core/sfx2/source/doc/sfxbasemodel.cxx:3207 at /home/buildslave/source/libo-core/sfx2/source/doc/sfxbasemodel.cxx:1783.

(line 30: oReport.storeToURL(stUrl, arg()))

Not sure why this happens. I searched online and this is the only post with answers that has a similar scenario (in his situation because of a bad FilterName). There are other posts about using storeAsURL instead of storeToURL, but in the example you modified it’s already using storeAsURL.

Hello,
The error is stating the location is incorrect. I can duplicate with an incorrect location.
.

Can’t state what your problem is as you do no state the OS you use (tried searching previous questions) and you have not shown the line of code and what you changed.
.
My test was on Ubuntu 20 and the line begins with:

stUrl = "file:///home

but that , if you also are on Linux, is not present in your error message and should be.
.
Edit:
.
More searching in your old posts show, at least at one point, your OS to be Manjaro.
.
With that, I believe (don’t have Manjaro to test with) the URL line should be:
.
stUrl = “file:///home/alberto/Documentos/Invoices/” & sInvoice
.
This is based upon the error message provided. If any directory was spelled incorrectly to begin with it still needs to be corrected.

Thanks a lot! It worked!! I’ll adapt it to its newest version and will add it to directly to the invoices form :blush: