[Solved] CALC: macro export to pdf with cell as filename

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
josoetgebrook
Posts: 6
Joined: Wed Dec 09, 2009 1:21 pm

[Solved] CALC: macro export to pdf with cell as filename

Post by josoetgebrook »

I found the topic http://user.services.openoffice.org/en/ ... =20&t=7280 but I don't know how te change it to export to pdf.

I want range A1:G50 from tab 3 to be exported to pdf.
The file has to be saved as the content of cell G11 from tab 3

If possible I like the option to add a button in the document (outside range A1:G50) which makes it possible that the macro will be executed by clicking the button.

Can someone help me?
Last edited by josoetgebrook on Fri Dec 11, 2009 1:09 am, edited 1 time in total.
Openoffice 3.1.1 Windows XP
rudolfo
Volunteer
Posts: 1488
Joined: Wed Mar 19, 2008 11:34 am
Location: Germany

Re: CALC: macro export to pdf with cell as filename

Post by rudolfo »

I haven't really looked at that other macro, that you referenced here. And I don't know how this macro accomplishes it to export only certain columns to pdf. But as you had a precise question,
on how to change the export from csv to pdf, I add here what I found in Roberto Benitez's BaseTools extension.

Code: Select all

  FilterProps(0).Name="FilterName";
   FilterProps(0).Value="writer_pdf_Export";
   Doc.storeToURL(sFile,FilterProps())

Notice that it uses .storeToURL(). storeTo means "Export", while storeAs is the macro counterpart for the GUI.
OpenOffice 3.1.1 (2.4.3 until October 2009) and LibreOffice 3.3.2 on Windows 2000, AOO 3.4.1 on Windows 7
There are several macro languages in OOo, but none of them is called Visual Basic or VB(A)! Please call it OOo Basic, Star Basic or simply Basic.
josoetgebrook
Posts: 6
Joined: Wed Dec 09, 2009 1:21 pm

Re: CALC: macro export to pdf with cell as filename

Post by josoetgebrook »

Making a macro that saves that range is not the problem, I already made that.
But with the macro I made, the file will allways save with the same filename.

Code: Select all

args2(0).Name = "URL"
args2(0).Value = "file:///K:/Documents/file.pdf"
args2(1).Name = "FilterName"
args2(1).Value = "calc_pdf_Export"
I like "file:///K:/Documents/file.pdf" to be replaced by "file:///K:/Documents/content cell G11.pdf"
Openoffice 3.1.1 Windows XP
rudolfo
Volunteer
Posts: 1488
Joined: Wed Mar 19, 2008 11:34 am
Location: Germany

Re: CALC: macro export to pdf with cell as filename

Post by rudolfo »

Okay,

in this case you might have a look at this post: viewtopic.php?f=45&t=24041
Look for oSheet.getCellByPosition(5,11).getString() to see how to extract a filename from the content of a cell (F12) in a spreadsheet. (I don't think that you have problems to figure out how to get the sheet object oSheet. If I am wrong you can find that in the mentioned thread -- or at a hundred other places in this forums or in the macro guide).

You probably want to have OS filenames in your sheet, like E:\Documents\file.pdf.
Simply feed them to convertToURL():

Code: Select all

 sURL = ConvertToURL("E:\Documents\file.pdf")
You don't need to construct the full URI "file:///E:/...." on your own.
 Edit: I just had a quick look at the other thread, that you mentioned in your first post: That post has in the first reply the same typical lines of code for reading the content of a cell and using that to build a filename.
That's why I was irritated by your answer that you know how to save a range into a pdf file, but that you don't know how to make the pdf filename a variable based on the content of a cell. 
OpenOffice 3.1.1 (2.4.3 until October 2009) and LibreOffice 3.3.2 on Windows 2000, AOO 3.4.1 on Windows 7
There are several macro languages in OOo, but none of them is called Visual Basic or VB(A)! Please call it OOo Basic, Star Basic or simply Basic.
josoetgebrook
Posts: 6
Joined: Wed Dec 09, 2009 1:21 pm

Re: [Solved] CALC: macro export to pdf with cell as filename

Post by josoetgebrook »

Thanks, it's solved.

The code I have is:

Code: Select all

REM  *****  BASIC  *****

Sub Main

End Sub


sub export_pdf
rem ----------------------------------------------------------------------
rem define variables
dim document   as object
dim dispatcher as object
rem ----------------------------------------------------------------------
rem get access to the document
document   = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")

rem ----------------------------------------------------------------------
dim args1(0) as new com.sun.star.beans.PropertyValue
args1(0).Name = "ToPoint"
args1(0).Value = "$A$1:$G$56"

dispatcher.executeDispatch(document, ".uno:GoToCell", "", 0, args1())

rem ----------------------------------------------------------------------
dim args2(2) as new com.sun.star.beans.PropertyValue
  Dim Cel        As Object
  Dim Werkblad    As Object
  Dim Werkmap    As Object
  
  Werkmap=ThisComponent
  Werkblad=Werkmap.Sheets.getByName("Factuur")
  Cel=Werkblad.getCellByposition(6,10) 'G11


args2(0).Name = "URL"
args2(0).Value = "file:///e:/Documents/"& Cel.getString()&".pdf"
args2(1).Name = "FilterName"
args2(1).Value = "calc_pdf_Export"
args2(2).Name = "FilterData"
args2(2).Value = Array(Array("UseLosslessCompression",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("Quality",0,90,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("ReduceImageResolution",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("MaxImageResolution",0,300,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("UseTaggedPDF",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("SelectPdfVersion",0,0,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("ExportNotes",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("ExportBookmarks",0,true,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("OpenBookmarkLevels",0,-1,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("UseTransitionEffects",0,true,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("IsSkipEmptyPages",0,true,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("IsAddStream",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("FormsType",0,0,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("ExportFormFields",0,true,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("HideViewerToolbar",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("HideViewerMenubar",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("HideViewerWindowControls",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("ResizeWindowToInitialPage",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("CenterWindow",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("OpenInFullScreenMode",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("DisplayPDFDocumentTitle",0,true,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("InitialView",0,0,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("Magnification",0,0,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("Zoom",0,100,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("PageLayout",0,0,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("FirstPageOnLeft",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("InitialPage",0,1,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("Printing",0,2,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("Changes",0,4,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("EnableCopyingOfContent",0,true,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("EnableTextAccessForAccessibilityTools",0,true,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("ExportLinksRelativeFsys",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("PDFViewSelection",0,0,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("ConvertOOoTargetToPDFTarget",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("ExportBookmarksToPDFDestination",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("_OkButtonString",0,"",com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("EncryptFile",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("DocumentOpenPassword",0,"",com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("RestrictPermissions",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("PermissionPassword",0,"",com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("Selection",0,,com.sun.star.beans.PropertyState.DIRECT_VALUE))

dispatcher.executeDispatch(document, ".uno:ExportToPDF", "", 0, args2())


end sub
Can you please tell my how I can make it work to remove the drivelettre. If I remove it, I get an error-message.

Now I just have one other question about the selection:
http://user.services.openoffice.org/en/ ... 35&start=0
Openoffice 3.1.1 Windows XP
rudolfo
Volunteer
Posts: 1488
Joined: Wed Mar 19, 2008 11:34 am
Location: Germany

Re: [Solved] CALC: macro export to pdf with cell as filename

Post by rudolfo »

josoetgebrook wrote:Can you please tell my how I can make it work to remove the drivelettre. If I remove it, I get an error-message.
I see in your macro code that you have

Code: Select all

 args2(0).Name = "URL"
 args2(0).Value = "file:///e:/Documents/" & Cel.getString() & ".pdf"
The URI needs to have a full path specification, because the protocol identifier "file://" can not be left out. A full path on a MS Windows system is always with the drive letter. If you leave it out, the URI is not complete and the only logical response on that is an error.

The drive letter e: is only in your macro code, but the user of the macro simply needs to enter a basename (if she is supposed to make this choice). In other words the drive letter is hidden in your macro code and the user need not to bother with it and should be happy. What more do you want?

Or is it that "E:\Documents\" is not generic enough? Some of the potential users of your macro will only have drive C: and D: and they will need "D:\Documents\" instead? Or other users will run this on a Unix system and have only "/home/theuser".
In this case we hit a completely different area ... and this is portability. If you can't use relative paths you will have to put a lot of effort in this. But this problem is not specific for OpenOffice. You have the same questions and obstacles for any other application.
Typically you use Environment variables or strictly follow guidelines where to store files which is /home/theuser /.somedir for Unix or the famous C:\Documents and Settings\theuser\My Documents on MS Windows up to Win XP.

And I can only repeat use ConvertToURL. Have you checked what happens if the cell's content (or the basename of the pdf file) are two words and a space in between?
Better use:

Code: Select all

 args2(0).Value = ConvertToURL("e:\Documents\" & Cel.getString() & ".pdf")
OpenOffice 3.1.1 (2.4.3 until October 2009) and LibreOffice 3.3.2 on Windows 2000, AOO 3.4.1 on Windows 7
There are several macro languages in OOo, but none of them is called Visual Basic or VB(A)! Please call it OOo Basic, Star Basic or simply Basic.
josoetgebrook
Posts: 6
Joined: Wed Dec 09, 2009 1:21 pm

Re: [Solved] CALC: macro export to pdf with cell as filename

Post by josoetgebrook »

The reason I don't want a drivelettre is that the file is stored on a USB-drive which is used in different computers.

Is there a possibility to do it like in html:

Code: Select all

src="/folder/filename.pdf"
Openoffice 3.1.1 Windows XP
rudolfo
Volunteer
Posts: 1488
Joined: Wed Mar 19, 2008 11:34 am
Location: Germany

Re: [Solved] CALC: macro export to pdf with cell as filename

Post by rudolfo »

As I mentioned in my previous post, you can either specify an absolute pathname in operating system's notation (E:\Documents\file.pdf or /home/user/file.pdf) or in URL notation (file:///E:/Documents/file.pdf or file:///home/user/file.pdf). In either case you need the drive letter on the windows system or the mount point on the Unix system.

So the best you can do is trying to guess the drive letter that is assigned on each computer to the USB drive. That would require:
  • List all drive letters on the computer (including their labels)
  • Scan through the labels and look for signifant descriptions like "USB something"
  • Hope that you find only one drive that matches these criterias ... and use it in your macro
I think I heard that on some MS Windows systems you can use tools to force a USB device to always use the same drive letter. This could make the process easier. But definetly this is nothing that you can achieve with the OOo Basic macro language. It might be possible with python or Java ...

But frankly "finding out which drive letter is assigned to my usb device" is not something I would ask in an OpenOffice forum :?
OpenOffice 3.1.1 (2.4.3 until October 2009) and LibreOffice 3.3.2 on Windows 2000, AOO 3.4.1 on Windows 7
There are several macro languages in OOo, but none of them is called Visual Basic or VB(A)! Please call it OOo Basic, Star Basic or simply Basic.
jim7fl
Posts: 5
Joined: Fri Feb 12, 2010 12:44 am

Re: [Solved] CALC: macro export to pdf with cell as filename

Post by jim7fl »

Making an assumption you're using windows and have several other windows computers using the same usb device.

Why not Map a Drive Letter on each computer to be the usb device and use the same drive letter for each.

Then, no matter which computer to which it is attached, the files will all have the same root drive letter.
Jim Smith / Linux Mint 19.3 LibreOffice Version: 6.0.7.3
on the banks of the Suwannee River
White Springs, Florida USA
Post Reply