La Web del Programador: Comunidad de Programadores
 
    Pregunta:  35822 - JUSTIFICAR TEXTO EN LA IMPRESORA
Autor:  Xavi
Como puedo justificar un campo de texto (memo) en la impresora?

Gracias

  Respuesta:  Xavi
Navegando por la red, he encontrado este codigo:

Sub justifica_printer(x0, xf, y0, txt)
' x0, xf = posicion de los margenes izquierdo y derecho
' y0 = posicion vertical donde se desea empezar a escribir
' txt = texto a escribir

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

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

While s <> ""

ss = ""
While (s <> "") And (Printer.TextWidth(ss) <= ancho)
ss = ss & Left$(s, 1)
s = Right$(s, Len(s) - 1)
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) <> " ") 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 & "" <> "") 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

End Sub

Daniel Castillo Martinez, [email protected]