[Solved] Name PDF by Cell Value

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
Gonzo714
Posts: 47
Joined: Wed Nov 23, 2016 2:52 pm

[Solved] Name PDF by Cell Value

Post by Gonzo714 »

I found this PDF print macro that does my needs, however I need to alter the output file name per customer/sheet.
(credit to source -- https://stackoverflow.com/questions/305 ... o-pdf-file)
 Edit: The link above produces a Page Not Found error -- MrProgrammer, forum moderator  

sub storeCellRangeToPDF()

oDoc = ThisComponent
oController = oDoc.getCurrentController()
oSheet = oController.getActiveSheet()
oCellRange = oSheet.getCellRangeByName("$A$1:$B$3")

dim aFilterData(0) as new com.sun.star.beans.PropertyValue
aFilterData(0).Name = "Selection"
aFilterData(0).Value = oCellRange

dim aMediaDescriptor(1) as new com.sun.star.beans.PropertyValue
aMediaDescriptor(0).Name = "FilterName"
aMediaDescriptor(0).Value = "calc_pdf_Export"
aMediaDescriptor(1).Name = "FilterData"
aMediaDescriptor(1).Value = aFilterData()

oDoc.storeToURL("file:///home/axel/Dokumente/test.pdf", aMediaDescriptor())

end sub




Can the last line be altered to name the file by cell value(s)?
I.E. ---> oDoc.storeToURL("A1") .... or even better --> oDoc.storeToURL("A1&A2&A3")


...and since im here, and If y'all feel ambitious - can the named PDF file be added to a KWrite email with its values derived from the sheets cell values?
To Email, From Email, Subjuct, Body, Signature - (B10, C10, D10, E10, F10)


thanx


P.S., Im running Libre 7.5.6.2 with a fresh install of Debian 12,onto a new hdd. Your server however doesn't believe me.
Last edited by robleyd on Sun Oct 08, 2023 1:05 am, edited 2 times in total.
Reason: Tag [Solved]
LibreOffice 7.5, Debian 12
FJCC
Moderator
Posts: 9316
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Name PDF by Cell Value

Post by FJCC »

If you want to construct the file name from the strings in cells, you can get the cells with getCellrangeByName() and then get the String property. Here is a sketch of the new code

Code: Select all

Cell_A1 = oSheet.getCellRangeByName("A1")
Cell_A2 = oSheet.getCellRangeByName("A2")
FileName = Cell_A2.String & Cell_A1.String
oDoc.storeToURL(FileName, aMediaDescriptor())
If none of the cells contain the .pdf file name extension, remember to add that to FileName
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
Gonzo714
Posts: 47
Joined: Wed Nov 23, 2016 2:52 pm

Re: Name PDF by Cell Value

Post by Gonzo714 »

Works like a charm. Thank You.

The following can be substituted without much effort, but just for the sake of discussion....
Seems macros dont like the # symbol, is there a work around for this to be displayed within the filename?

file:///home/mylinux/Documents/BobsPO#1234.pdf

I appreciate your time and effort.
LibreOffice 7.5, Debian 12
FJCC
Moderator
Posts: 9316
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Name PDF by Cell Value

Post by FJCC »

In what way is # a problem. Is it not being read from the cell or is it not accepted as part of the file name when you write the file?
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
User avatar
Zizi64
Volunteer
Posts: 11382
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Name PDF by Cell Value

Post by Zizi64 »

Seems macros dont like the # symbol, is there a work around for this to be displayed within the filename?
Please upload an ODF type sample file with the embedded macro code.
Last edited by Zizi64 on Sat Oct 07, 2023 9:07 am, edited 1 time in total.
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
User avatar
MrProgrammer
Moderator
Posts: 4956
Joined: Fri Jun 04, 2010 7:57 pm
Location: Wisconsin, USA

Re: Name PDF by Cell Value

Post by MrProgrammer »

Gonzo714 wrote: Fri Oct 06, 2023 10:33 pm Seems macros dont like the # symbol, is there a work around for this to be displayed within the filename?
# is allowed in a file name, but you are using the storeToURL() method in your macro and you must not pass a file name when a URL is required. Your macro is faulty if A1 and A2 contain a file name instead of a URL.

# is a reserved character in a URL. Use %23 in a URL to represent a #. Perhaps you can use UNO method getFileURLFromSystemPath() or StarBasic function ConvertToURL() to do the conversion for you. Or if # is the only forbidden character in your file name, set Z1 to =SUBSTITUTE(A2&A1;"#";"%23") and pass Z1 to your macro.
interface XFileIdentifierConverter

The link in your first post fails; try it.
Mr. Programmer
AOO 4.1.7 Build 9800, MacOS 13.6.3, iMac Intel.   The locale for any menus or Calc formulas in my posts is English (USA).
Gonzo714
Posts: 47
Joined: Wed Nov 23, 2016 2:52 pm

Re: Name PDF by Cell Value

Post by Gonzo714 »

by saying "macros dont like the # symbol," I forgot to specify - the feature works fine but refuses to output the # symbol or anything past it. But substituting # for another symbol works perfect.

Work-arounds are satisfactory for my needs.

original question SOLVED :super:
LibreOffice 7.5, Debian 12
Post Reply