[Solved] Update Macro to save the current print range to pdf

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
cmeylaq
Posts: 34
Joined: Wed Apr 19, 2023 3:52 pm

[Solved] Update Macro to save the current print range to pdf

Post by cmeylaq »

Hello all. I need to update the macro called when the "Close Shift" button is pressed to save the active sheet to pdf. All sheets have a print range set. I would like to save the pdf in a specified location with the sheet name as the file name.

Can anybody help me out please? Your help is truly appreciated.
Attachments
Shift Reconciliation v4 - Copy.ods
(30.56 KiB) Downloaded 55 times
Last edited by Hagar Delest on Thu Jun 08, 2023 10:59 pm, edited 1 time in total.
Reason: tagged solved.
OpenOffice 3.1 on Windows Vista
User avatar
Zizi64
Volunteer
Posts: 11359
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Update Macro to save the current print range to pdf

Post by Zizi64 »

Google is your friend:
if youare use a virtual pdf printer software:
viewtopic.php?t=35043

If you are using the built-in PDF export function:
viewtopic.php?t=79013
viewtopic.php?t=25480
https://www.google.hu/search?q=openoffi ... nt=gws-wiz
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.
cmeylaq
Posts: 34
Joined: Wed Apr 19, 2023 3:52 pm

Re: Update Macro to save the current print range to pdf

Post by cmeylaq »

Thank you Zizi. What do you recommend i use? Virtual pdf or export pdf?
OpenOffice 3.1 on Windows Vista
User avatar
Zizi64
Volunteer
Posts: 11359
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Update Macro to save the current print range to pdf

Post by Zizi64 »

What do you recommend i use? Virtual pdf or export pdf?
It depends on your knowledge on AOO/LO API. (API: Application Programming Inteface)
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
Zizi64
Volunteer
Posts: 11359
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Update Macro to save the current print range to pdf

Post by Zizi64 »

_____________________________________________
OpenOffice 3.1 on Windows Vista
Please update your signature in this forum.
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
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Update Macro to save the current print range to pdf

Post by Villeroy »

Print active sheet's set up print areas. Print used area if no areas are setup.

Code: Select all

REM  *****  BASIC  *****
Sub Main()
shx = ThisComponent.getSheets()
bMod = ThisComponent.isModified()
iUB = shx.getCount()-1
view = ThisComponent.getCurrentController()
ash = view.getActiveSheet()
ish = ash.RangeAddress.Sheet
sURL = ThisComponent.URL &"."& replace(ash.getName()," ","%20") &".pdf"
dim aOptions(0) as new com.sun.star.beans.PropertyValue
aOptions(0).Name = "FilterName"
aOptions(0).Value = "calc_pdf_Export"
REM remove print areas of all sheets
dim a(iUB )
for i = 0 to iUB
	sh = shx.getByIndex(i)
	a(i) = sh.PrintAreas
	sh.PrintAreas = Array()
next
REM reset active sheet's print areas and save
if uBound(a(ish))>-1 then
	ash.PrintAreas = a(ish)
else
	ash.PrintAreas = Array(getUsedRange(ash).getRangeAddress())
endif 
ThisComponent.storeToURL( sURL, aOptions())
REM restore print areas 	
for i = 0 to iUB
	sh = shx.getByIndex(i)
	sh.PrintAreas = a(i)
next
ThisComponent.setModified(bMod)
End Sub

Function getUsedRange(oSheet)
Dim oCursor1, oCursor2, addr
	oCursor1 = oSheet.createCursorByRange(oSheet)
	oCursor1.gotoStart()
	addr = oCursor1.getRangeAddress()
	oCursor2 = oSheet.createCursorByRange(oSheet)
	oCursor2.gotoEnd()
	addr.EndColumn = oCursor2.RangeAddress.EndColumn
	addr.EndRow = oCursor2.RangeAddress.EndRow
	getUsedRange = oSheet.getCellRangeByPosition(addr.StartColumn, addr.StartRow, addr.EndColumn,  addr.EndRow)
End Function
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
cmeylaq
Posts: 34
Joined: Wed Apr 19, 2023 3:52 pm

Re: Update Macro to save the current print range to pdf

Post by cmeylaq »

Thank you Villeroy.
I have set a print range for every sheet.
Which part of your code do i have 2 remove since the range is already defined?
OpenOffice 3.1 on Windows Vista
User avatar
Zizi64
Volunteer
Posts: 11359
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Update Macro to save the current print range to pdf

Post by Zizi64 »

Which part of your code do i have 2 remove since the range is already defined?
If it is a real question for you, then I suggest you: Do not remove anything.
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
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Update Macro to save the current print range to pdf

Post by Villeroy »

The macro removes all set up print areas, restores the print area(s) of the active sheet as the only area(s) to be printed and exports to pdf. FInally it restores all set up print areas, so the document should be the same as before starting the macro.

If the active sheet has no print area(s) set up, the used range will be set as a temporary print area.

P.S. As always, you are the one to test this macro thoroughly with
a copy
of your original document(s).
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
cmeylaq
Posts: 34
Joined: Wed Apr 19, 2023 3:52 pm

Re: Update Macro to save the current print range to pdf

Post by cmeylaq »

Thank you all for your feedback. managed to do it with your help.
OpenOffice 3.1 on Windows Vista
Post Reply