La Web del Programador: Comunidad de Programadores
 
    Pregunta:  37066 - IMPRESION JUSTIFICADA
Autor:  Horacio Elias
Es mi intención realizar una aplicación que lea informacion desde una base mdb y linea por linea la vaya imprimiendo en una hoja. Pero quiero que esta impresión salga justifica en ambos márgenes al estilo de Word.
¿Se puede?.
Gracias por vuestra ayuda

  Respuesta:  Xavi
Este código, me lo descarge de internet y le he realizado algunas modificaciones para controlar los saltos de linea, etc...
-----------------
Function justifica_printer(x0 As Long, xf As Long, y0 As Long, Txt As String) As Long
' x0, xf = posicion de los margenes izquierdo y derecho
' y0 = posicion vertical donde se desea empezar a escribir
' txt = texto a escribir
' devuelve la posicion vertical final

Dim x, y, k, ancho
Dim s As String, ss As String
Dim x_spc
Dim XX As Boolean

s = Txt
x = x0
y = y0
ancho = (xf - x0)

While s <> ""
ss = ""
XX = True
While (s <> "") And (Printer.TextWidth(ss) <= ancho) And XX = True
If Left$(s, 2) = Chr(13) & Chr(10) Then
s = Right$(s, Len(s) - 2)
XX = False
Else
ss = ss & Left$(s, 1)
s = Right$(s, Len(s) - 1)
End If
Wend
If (Printer.TextWidth(ss) > ancho) Then
s = Right$(ss, 1) & s
ss = Left$(ss, Len(ss) - 1)
End If
' aqui tenemos en ss lo maximo que cabe en una linea
If Right$(ss, 1) = " " Then
ss = Left$(ss, Len(ss) - 1)
Else
If (InStr(ss, " ") > 0) And (Left$(s & " ", 1) <> " ") And XX = True Then
While Right$(ss, 1) <> " "
s = Right$(ss, 1) & s
ss = Left$(ss, Len(ss) - 1)
Wend
ss = Left$(ss, Len(ss) - 1)
End If
End If
x_spc = 0
x = x0
If (Len(ss) > 1) And (s & "" <> "") And XX = True Then
x_spc = (ancho - Printer.TextWidth(ss)) / (Len(ss) - 1)
End If
Printer.CurrentX = x
Printer.CurrentY = y

If x_spc = 0 Then
Printer.Print ss;
Else
For k = 1 To Len(ss)
Printer.CurrentX = x
Printer.Print Mid$(ss, k, 1);
x = x + Printer.TextWidth("*" & Mid$(ss, k, 1) & "*") - Printer.TextWidth("**")
x = x + x_spc
Next
End If

y = y + Printer.TextHeight(ss)
While Left$(s, 1) = " "
s = Right$(s, Len(s) - 1)
Wend
Wend
justifica_printer = y
End Function