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?