How to insert footer text for printing with a macro?

I’m trying to insert a footer when printing a report using a macro. Although I can see the text in the object’s properties, the information does not appear on the printout.
.
Macros created where I’d like them to work:

Option Explicit
Option Compatible
REM -----------------------------------------------------------------------------
'''
'	Ajuda com configurações de impressão em: https://api.libreoffice.org/docs/idl/ref/namespacecom_1_1sun_1_1star_1_1view.html#a12ab04987d08416f8347a9790c7abf3e
'''
Private Sub ImprimirRelatorio(Optional sPrinter$, Optional iPaperFormat% = 1, Optional iCopies% = 1, Optional sPage$)
Dim oFrame			As Object
Dim oDispatcher		As Object
Dim myPrinter(2)	As New com.sun.star.beans.PropertyValue
Dim args(3)			As New com.sun.star.beans.PropertyValue

	If isMissing(sPrinter) Then sPrinter = EscolherImpressora

	If isMissing(sPage) Then sPage = "1"
	
	REM A visualização da impressão antes para o usuário imprimir manualmente não é suportada no momento.
	REM Então optei por fazer a configuração via macro
	REM oFrame = ThisComponent.getCurrentController.getFrame()
	REM oDispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
	REM oDispatcher.executeDispatch(oFrame, "uno:PrintPreview", "", 0, Array() )
	
	REM Configura opcoes de impressao
	myPrinter(0).Name = "Name"				'	Nome da impressora
	myPrinter(0).Value = sPrinter
	myPrinter(1).Name = "PaperOrientation"	'	Orientação da folha 0 = vertical, 1 = horizontal
	myPrinter(1).Value = 1
	myPrinter(2).Name = "PaperFormat"		'	Tamanho do papel 1 = A4, 4 = Carta, 5 = Ofício
	myPrinter(2).Value = iPaperFormat
	
	ThisComponent.setPrinter(myPrinter)		'	Define as opções de impressão
	
	args(0).Name = "CopyCount"				'	Número de copias
	args(0).Value = iCopies
	args(1).Name = "Collate"				'	Agrupar
	args(1).Value = True
	args(2).Name = "Pages"					'	Número da página para impressão
	args(2).Value = sPage
	args(3).Name = "Wait"					'	Aguarda a impressão ser concluida antes de retornar
	args(3).Value = True
	
	REM Adiciona ao relatorio nome do usuario logado
	InserirUsuarioRodape
	
	ThisComponent.Print(args())				'	Imprimir
	
End Sub	'	BasicProject.BP_Pendentes.ImprimirRelatorio

REM -----------------------------------------------------------------------------
'''
'	Insere data/hora e usuario logado, no rodapé do relatório gerado
'	Resultado gracas a exemplo no livro de Andrew Pitonyak (2016, p. 343) e ferramenta de inspeção de objetos
'''
Private Sub InserirUsuarioRodape(Optional oSheet As Object)
Dim oStyle			'	Estilo de página atual
Dim oFooterText		'	Rodapé alinhado a esquerda da página direita
Dim oCursor			'	Cursor para inserir texto
Dim s$				'	Variavel de trabalho
	
	If isMissing(oSheet) Then oSheet = ThisComponent.CurrentController.getActiveSheet()
	
	REM Obtem nome do estilo de página e depois o objeto para acessar suas propriedades
	s = oSheet.PageStyle
	oStyle = ThisComponent.StyleFamilies.getByName("PageStyles").getByName(s)

	REM Obtem nome do usuario logado na máquina para gerar um carimbo data/hora e usuario
	Select Case GetGUIType()
		Case 1 : s = "USERNAME"
		Case 4 : s = "USER"
	End Select
	
	s = "Gerado em " & Date(Now()) & " às " & Time(Now()) & ", por " & Environ(s)
	
	REM Acessa objeto Texto para usar servico TextCursor, que é utilizado para percorrer o objeto texto
	oFooterText = oStyle.RightPageFooterContent.getLeftText()
	
	oCursor = oFooterText.createTextCursor()
	oCursor.goToStart(False)
	
	REM Insere o texto do rodapé
	oFooterText.insertString(oCursor, s, False)
	
End Sub	'	BasicProject.BP_Pendentes.InserirUsuarioRodape

.
------------------SAMPLE------------------
Sample file
.
I still don’t understand exactly why it’s not working. Could someone point me in the right direction?

You were completely wrong to combine these two actions into one line.
Just try to get oThisStyle.RightPageFooterContent into a separate variable, then perform all the necessary manipulations and at the very end return the modified content to its place
oStyle.RightPageFooterContent = <your temporary separate variable>

2 Likes

See also: the API reference also tells that:

After changing the footer text contents, this property has to be reinserted into the property set.

1 Like

Got it @JohnSUN . I have a vague idea why. However, would you mind explaining further, just for knowledge?
.
And, with the adjustments indicating, the macro “InserirUsuarioRodape” would something like this, right?

Private Sub InserirUsuarioRodape(Optional oSheet As Object)
Dim oStyle			'	Estilo de página atual
Dim oFooterContent	'	Rodape da página direita
Dim oCursor			'	Cursor para inserir texto
Dim oLeftFooter		'	Rodape alinha a esquerda
Dim s$				'	Variavel de trabalho
	
	If isMissing(oSheet) Then oSheet = ThisComponent.CurrentController.getActiveSheet()
	
	REM Obtem nome do estilo de página e depois o objeto para acessar suas propriedades
	s = oSheet.PageStyle
	oStyle = ThisComponent.StyleFamilies.getByName("PageStyles").getByName(s)
	
	REM Obtem nome do usuario logado na máquina para gerar um carimbo data/hora e usuario
	Select Case GetGUIType()
		Case 1 : s = "USERNAME"
		Case 4 : s = "USER"
	End Select
	
	s = "Gerado em " & Date(Now()) & " às " & Time(Now()) & ", por " & Environ(s)
	
	REM Acessa objeto rodapé e depois objeto Texto para usar o servico TextCursor,
	REM que é utilizado para percorrer o objeto texto
	oFooterContent = oStyle.RightPageFooterContent
	oLeftFooter = oFooterContent.getLeftText()
	oCursor = oLeftFooter.createTextCursor()
	oCursor.goToStart(False)
	
	REM Insere o texto do rodapé
	oLeftFooter.insertString(oCursor, s, False)
	
End Sub	'	BasicProject.BP_Pendentes.InserirUsuarioRodape

.
@mikekaganski
Considering the remark, the right method to use would be insertTextContent from xText interface?

I meant something like this

Dim oContent	
.....
	s = "Gerado em " & Date(Now()) & " às " & Time(Now()) & ", por " & Environ(s)
	REM Acessa objeto Texto para usar servico TextCursor, que é utilizado para percorrer o objeto texto
	oContent = oStyle.RightPageFooterContent
	oFooterText = oContent.getLeftText()
	oCursor = oFooterText.createTextCursor()
	oCursor.goToStart(False)
	
	REM Insere o texto do rodapé
	oFooterText.insertString(oCursor, s, False)
	oStyle.RightPageFooterContent = oContent 

In other words, everything is the same as what you did yourself, but the oStyle.RightPageFooterContent = oContent line completes the action (it’s not in your code)

Now I really understand Mike’s comment. Thank you very much for your help @JohnSUN and @mikekaganski !