How To Print Named Ranges out in Libre Calc

I thought it would be a good trick to name a couple of ranges on my spreadsheet and then I could print them out individually whenever i wanted one of them. But I can’t find how to do. Name ranges, yet, but print them, no.
How?

menu:Sheet>Names>Insert [Paste All]

that pastes the range in at the cursor position I suppose. it doesn’t print it does it?

Maybe Data > Group and Outline > Group would work better than named ranges. You can easily hide or reveal pre-selected columns and print.

Otherwise the Calc Guide explains how to use named ranges for printing in the Chapter on printing. Currently, starting from page 242, Chapter 7 for Calc Guide 7.5

And a slightly different way for Named range.

  1. Select cells via name box.
  2. In Print Dialog: Range and Copies / More From which : Print Selected Cells
1 Like

And here is a macro solution in a sample file:

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

option explicit

Sub PrintNamedArea

 Dim oDoc as Object
 Dim oCurrCtrl as object
 Dim oSheet as object
 Dim oCellRange as object
 Dim oRangeAddress as object
 Dim aOrigPrintAreas() as variant
 Dim aTempPrintAreas(0) as variant
 Dim iSheetNr as integer
 Dim iPrAreasCount as integer
 Dim sRangeName as string
 
	oDoc = ThisComponent
	oCurrCtrl = oDoc.CurrentController
	sRangeName = inputbox("Please type in the name of the range to print", "Range name", "Named1")
	
	if oDoc.namedranges.hasByName(sRangeName) then 
		oCellRange = oDoc.NamedRanges.getByName(sRangeName).ReferredCells
		'' xray oCellRange 
		oRangeAddress = oCellRange.getRangeAddress
		iSheetNr = oRangeAddress.Sheet 
		'' print  iSheetNr
		oSheet = oDoc.Sheets.getbyIndex(iSheetNr)
		oCurrCtrl.setActiveSheet(oSheet)
		'' xray oSheet
		
		aOrigPrintAreas() = oSheet.GetPrintAreas() '' get the original Print areas
		'' xray aOrigPrintAreas()	
		iPrAreasCount = uBound(aOrigPrintAreas)-lBound(aOrigPrintAreas)+1 '' Check the existing print areas 
		MsgBox( "There are " & iPrAreasCount & " defined Print Ranges BEFORE the Print")	
		
		aTempPrintAreas(0) = oRangeAddress
		oSheet.setPrintAreas(aTempPrintAreas) '' set a print area
		
		aTempPrintAreas() = oSheet.GetPrintAreas() '' Check the  print areas to print 
		iPrAreasCount = uBound(aTempPrintAreas)-lBound(aTempPrintAreas)+1 
		MsgBox( "There is " & iPrAreasCount & " defined Print Range TO Print")
		oDoc.Print(Array())
		Wait 500
		
		oSheet.setPrintAreas(aOrigPrintAreas)  '' re-apply the original Print areas 
		
		aOrigPrintAreas() = oSheet.GetPrintAreas() '' Check the re-applied print areas 
		iPrAreasCount = uBound(aOrigPrintAreas)-lBound(aOrigPrintAreas)+1
		MsgBox( "There are " & iPrAreasCount & " defined Print Ranges AFTER the print")	
	else
		MsgBox( "Error: The named range not exist.")
		Exit sub
	end if	
 

End Sub


PrintNamedArea.ods (16.2 KB)

2 Likes
Sub PrintArea

text = Inputbox ("Inform the Area to print.", "Attention", "type here")
dim args1(0) as new com.sun.star.beans.PropertyValue
args1(0).Name = "ToPoint" : args1(0).Value = text
CreateUnoService("com.sun.star.frame.DispatchHelper") _
.executeDispatch(ThisComponent.CurrentController _
.Frame, ".uno:GoToCell", "", 0, args1())

CreateUnoService("com.sun.star.frame.DispatchHelper") _
.executeDispatch(ThisComponent.CurrentController _
.Frame, ".uno:DefinePrintArea", "", 0, Array())

CreateUnoService("com.sun.star.frame.DispatchHelper") _
.executeDispatch(ThisComponent.CurrentController _
.Frame, ".uno:PrintDefault", "", 0, Array())

End Sub 
1 Like

@ zizi64
Looks good. I’ll swot up on using macros. :slight_smile:

@shiavinatto
Also looks good but I wouldn’t know what to do with it. It’s for ‘specialists’ or a dumb user could employ it?

shiavinatto’s macro code looks like as a RECORDED macro. The Macro Recorder uses the Dispatcher. The Macro Recorder can not record all of the user’s activity.

It is better to use the API commands (functions and procedures) in a manually created macro code - like in my code.

@ zizi64

ahh… so… thanks for that… I’ll get down to it as soon as I can… :slight_smile:

Here is my code without the helper lines. These lines help you to understand the code, but they are not required for the proper working of the code:

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

option explicit

Sub PrintNamedArea

 Dim oDoc as Object
 Dim oCurrCtrl as object
 Dim oSheet as object
 Dim oCellRange as object
 Dim oRangeAddress as object
 Dim aOrigPrintAreas() as variant
 Dim aTempPrintAreas(0) as variant
 Dim iSheetNr as integer
 Dim iPrAreasCount as integer
 Dim sRangeName as string
 
	oDoc = ThisComponent
	oCurrCtrl = oDoc.CurrentController
	sRangeName = inputbox("Please type in the name of the range to print", "Range name", "Named1")	
	if oDoc.namedranges.hasByName(sRangeName) then 
		oCellRange = oDoc.NamedRanges.getByName(sRangeName).ReferredCells
		oRangeAddress = oCellRange.getRangeAddress
		iSheetNr = oRangeAddress.Sheet 
		oSheet = oDoc.Sheets.getbyIndex(iSheetNr)
		oCurrCtrl.setActiveSheet(oSheet)
		aOrigPrintAreas() = oSheet.GetPrintAreas()
		aTempPrintAreas(0) = oRangeAddress
		oSheet.setPrintAreas(aTempPrintAreas)
		oDoc.Print(Array())
		Wait 500
		oSheet.setPrintAreas(aOrigPrintAreas)
	else
		MsgBox( "Error: The named range not exist.")
		Exit sub
	end if
End Sub

Hello @Zizi64, I’m not disputing the quality of the macro writing, but giving an alternative to achieve the desired one.

1 Like