Esta solução gera um PDF temporário, e imprimi sem sair da Planilha Ativa.
This solution generates a temporary PDF and prints it without leaving the active worksheet.
Sub PrintAnotherSheet
Dim oDoc As Object
Dim oArea As Object
Dim oSheet As Object
Dim oSFDoc As Object
Dim oPDF As Object
Dim oDesktop As Object
Dim oEnd As Object
Dim sRange As String
Dim sPDF As String
Dim aLoad(0) As New com.sun.star.beans.PropertyValue
Dim aPrint(2) As New com.sun.star.beans.PropertyValue
oDoc = ThisComponent
'------------------------------------------------------
' Localiza SAIDAIMPRESSAO
'------------------------------------------------------
If Not oDoc.NamedRanges.hasByName("SAIDAIMPRESSAO") Then
MsgBox "A área SAIDAIMPRESSAO não foi encontrada.", _
48, "ImpressĂŁo"
Exit Sub
End If
oArea = oDoc.NamedRanges _
.getByName("SAIDAIMPRESSAO") _
.getReferredCells()
oSheet = oArea.getSpreadsheet()
oEnd = oArea.RangeAddress
'------------------------------------------------------
' Monta o endereço no formato:
' Planilha2.A1:H50
'------------------------------------------------------
sRange = oSheet.Name & "." & _
ConvertToLetter(oEnd.StartColumn) & _
(oEnd.StartRow + 1) & ":" & _
ConvertToLetter(oEnd.EndColumn) & _
(oEnd.EndRow + 1)
'------------------------------------------------------
' PDF temporário
'------------------------------------------------------
sPDF = Environ("TEMP") & "\SAIDAIMPRESSAO_TEMP.pdf"
If Dir(sPDF) <> "" Then Kill sPDF
'------------------------------------------------------
' ScriptForge Calc associado ao documento
'------------------------------------------------------
GlobalScope.BasicLibraries.LoadLibrary("ScriptForge")
oSFDoc = CreateScriptService("Calc", oDoc)
'------------------------------------------------------
' Exporta somente SAIDAIMPRESSAO
'------------------------------------------------------
If Not oSFDoc.ExportRangeToFile( _
sRange, _
sPDF, _
"pdf", _
True) Then
MsgBox "NĂŁo foi possĂvel criar o PDF.", _
48, "ImpressĂŁo"
oSFDoc.Dispose()
Exit Sub
End If
oSFDoc.Dispose()
'------------------------------------------------------
' Abre o PDF oculto
'------------------------------------------------------
oDesktop = createUnoService("com.sun.star.frame.Desktop")
aLoad(0).Name = "Hidden"
aLoad(0).Value = True
oPDF = oDesktop.loadComponentFromURL( _
ConvertToURL(sPDF), _
"_blank", _
0, _
aLoad())
If IsNull(oPDF) Or IsEmpty(oPDF) Then
MsgBox "NĂŁo foi possĂvel abrir o PDF.", _
48, "ImpressĂŁo"
Kill sPDF
Exit Sub
End If
'------------------------------------------------------
' Imprime o PDF
'------------------------------------------------------
aPrint(0).Name = "Wait"
aPrint(0).Value = True
aPrint(1).Name = "CopyCount"
aPrint(1).Value = 1
aPrint(2).Name = "Collate"
aPrint(2).Value = True
oPDF.print(aPrint())
'------------------------------------------------------
' Fecha o PDF
'------------------------------------------------------
oPDF.close(True)
'------------------------------------------------------
' Exclui o arquivo temporário
'------------------------------------------------------
If Dir(sPDF) <> "" Then Kill sPDF
End Sub
Function ConvertToLetter(nCol As Long) As String
Dim n As Long
Dim s As String
n = nCol + 1
Do While n > 0
n = n - 1
s = Chr(65 + (n Mod 26)) & s
n = Int(n / 26)
Loop
ConvertToLetter = s
End Function