[Solved] [Basic] Export Calc range to PDF

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
Bernard Mouille
Posts: 10
Joined: Fri Dec 31, 2021 9:42 pm

[Solved] [Basic] Export Calc range to PDF

Post by Bernard Mouille »

Hello,

I need to save a selected zone to pdf.
I have look a lot of forums but my code not works.
Any idea ?
Thanks,
Bernard.

My bad code :

Code: Select all

   ' Error in this code.

   option explicit

   ' Paramer : pdf file name to save.
   Const PdfFile = "file:///C:/BdPrg/CalcPrgVb/SaveAsPdf/Result.pdf"

   Dim oServiceManager
   Dim oDesktop
   Dim oDoc
   Dim oSheet
   Dim c
   Dim l
   Dim oCursor
   Dim oRange

   Set oServiceManager = CreateObject( "com.sun.star.ServiceManager" )
   Dim args1( 0 )
   Set args1( 0 )      = oServiceManager.Bridge_GetStruct( "com.sun.star.beans.PropertyValue" )
   args1( 0 ).name     = "Hidden"
   args1( 0 ).value    = False
   Set oDesktop        = oServiceManager.CreateInstance( "com.sun.star.frame.Desktop" )
   Set oDoc            = oDesktop.LoadComponentFromURL( "private:factory/scalc", "_blank", 0, args1 )
   Set oSheet          = oDoc.GetSheets.GetByIndex( 0 )

   l = 0 : c = -1
   c = c + 1 : oSheet.GetCellByPosition( c, l ).SetString( "Cell A1" )
   c = c + 1 : oSheet.GetCellByPosition( c, l ).SetString( "Cell B1" )

   l = l + 1 : c = -1
   c = c + 1 : oSheet.GetCellByPosition( c, l ).SetString( "Cell A2" )
   c = c + 1 : oSheet.GetCellByPosition( c, l ).SetString( "Cell B2" )

   Set oCursor = oSheet.createCursor()
   oCursor.gotoEndOfUsedArea( True )
   Set oRange = oSheet.getCellRangeByPosition( 0, 0, oCursor.getRangeAddress.EndColumn _
                                                   , oCursor.getRangeAddress.EndRow )
   oDoc.currentcontroller.select( oRange )

   Dim FilterProps(0)
   Set FilterProps(0)   = oServiceManager.Bridge_GetStruct( "com.sun.star.beans.PropertyValue" )
   FilterProps(0).Name  = "Selection"
   FilterProps(0).Value = oDoc.currentSelection

   Dim args2(1)
   Set args2(0)   = oServiceManager.Bridge_GetStruct( "com.sun.star.beans.PropertyValue" )
   Set args2(1)   = oServiceManager.Bridge_GetStruct( "com.sun.star.beans.PropertyValue" )
   args2(0).Name  = "FilterName"
   args2(0).Value = "calc_pdf_Export"
   args2(1).Name  = "FilterData"
   args2(1).Value = FilterProps

   oDoc.storeAsURL PdfFile, args2 'Error 800A03E9.

   msgbox( "Script end" )

   Set oServiceManager = Nothing
Last edited by MrProgrammer on Sat Jan 01, 2022 4:49 pm, edited 1 time in total.
Reason: Moved from External Programs forum to OpenOffice Basic, Python, BeanShell, JavaScript
libre Office 7.2.4.1(x64) Windows 8
JeJe
Volunteer
Posts: 2777
Joined: Wed Mar 09, 2016 2:40 pm

Re: Calc Ole Vbscript Export to Pdf

Post by JeJe »

Try

Code: Select all

XXXXXXXXXXXXXXXXXXXXXXXX
Scrap that. There's a working example here.

https://stackoverflow.com/questions/305 ... o-pdf-file

Edit: it uses storetoURL instead of storeasurl. Changing yours to that works for me from the Basic IDE
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Bernard Mouille
Posts: 10
Joined: Fri Dec 31, 2021 9:42 pm

Re: Calc Ole Vbscript Export to Pdf

Post by Bernard Mouille »

Many thanks, if works very good.
Have a happy new year,
Bernard.
libre Office 7.2.4.1(x64) Windows 8
Post Reply