How to set individual attachment name for mail merge macro

I just wanted to know how to set an individual attachment name for mail merge macro.

Normally you can set the name to a fixed name:
oMailMerge.AttachmentName = “inv.pdf”

But when I want to get the name from a column from the database individually for each data set?
For just printing it there is a simple solution but not for send as mail or?

oMailMerge.FileNameFromColumn = true
oMailMerge.Filenameprefix = “Column”

Maybe I have to declare a string first and then make a sql statement to save the name for each data set in the string?
Maybe someone can help.
Many thanks for the quick help here in the forum :slight_smile:


Will try to answer what I believe your question asks. Will also reference a previously asked question by yourself → How to print a base report and send it by mail (mailmerge)

In that previous answer, your sample code (noted in comment) used a query to retrieve the email address (field “mail_buyer”). The code in my sample used an SQL statement for the same purpose.

Your request here is the same. If you want the output file name as one stored in the database, you have that field output named in you query (or, as in my case, an SQL statement) and you specify that name:

oMailMerge.FileNameFromColumn = true
oMailMerge.Filenameprefix = "YOUR_COLUMN_NAME"

The name of the file should not contain an extension. Mail Merge will add the appropriate extension as to the output you have selected.

Have tested with .odt and .pdf files output.

Edit 2020-09-03:

Deleted edited section. Would allow retrieval but does not sync to mail merge.

Thanks for your quick reply!

oMailMerge.FileNameFromColumn = true
oMailMerge.Filenameprefix = "YOUR_COLUMN_NAME"

For me this is not working, only for file output, when I directly send it with mail this lines will be ignored and the filename will always be part_2.pdf
Documentiation Libre Office: ◆ FileNamePrefix …This property is only evaluated for file output.
Or am I doing something wrong? Maybe can you explain one more time how to make it with sql statement?
As i see in your example, there the attachement name is only Invoice_ and the actual date? Nothing from the database or?

sInvoice = "Invoice_" & sCust & "_" & FORMAT(Now, "YYYYMMDD") & ".odt"
oMailMerge.AttachmentName = Left(sInvoice,Len(sInvoice) - 3) & "pdf"


In my answer I wrote:

Will try to answer what I believe your question asks.

In your question I saw:

but not for send as mail

So it appears I am not understanding what you are looking for. Do you want to obtain a file name from a database file for file creation for saving on your computer or for naming the attachment on an email? Also since this is to clarify the question, what is this about:

For just printing it there is a simple solution…

Please provide clarification before I proceed further.

Okay sorry i think i did not describe it clearly: I want to name the attachment on an email based on a data set in the database. So for every Email the attachment name should be different based on the data in the database column. For just saving the file and not email it i can use “Filenameprefix”. But when mailmerge directly send it by mail this is not working.


Still need more clarity. Previous question was run from Base. This question is tagged with Writer. Is this to be run from Writer? It would appear this will require creating and running SQL from the macro. Are you familiar with this process?

Yes from writer with macro, first I have to complete this mailmerge writer project and afterwards in the next weeks I have to do the same with base reports, anyway.
So for now I want to do this in writer with mailmerge. I have no experience with running SQL from the macro but I think i will understand it :slight_smile:

Have no further answer at this point.

Okay no problem probably it is too complicated with writer, thank you anyway for the many and quick answers here in the forum, you are very willing to help, may god bless you :slight_smile: