Exportar a TXT con ancho fijo
Publicado por Javier (1 intervención) el 05/07/2004 19:06:34
Please, alguien me puedo decir como Exporto datos de un formulario a un fichero TXT con ancho fijo. Gracias.
Valora esta pregunta
0
Private Sub ExportarTXT_Click()
Dim filepath As String
Dim output As String
Dim i As Integer
' Ruta y nombre del archivo TXT
filepath = "C:\Ruta\Archivo.txt"
' Abrir el archivo en modo de escritura
Open filepath For Output As #1
' Recorrer los registros del formulario
With Me.RecordsetClone
.MoveFirst
Do Until .EOF
' Construir la línea de texto con ancho fijo
output = ""
output = output & PadRight(!Campo1, 10) ' Campo1 con ancho de 10 caracteres
output = output & PadRight(!Campo2, 15) ' Campo2 con ancho de 15 caracteres
' Continúa con los demás campos y sus anchos
' Escribir la línea en el archivo TXT
Print #1, output
.MoveNext
Loop
End With
' Cerrar el archivo
Close #1
MsgBox "Datos exportados correctamente."
End Sub
' Función para añadir espacios en blanco a la derecha del texto para alcanzar el ancho deseado
Function PadRight(text As String, width As Integer) As String
If Len(text) < width Then
PadRight = text & String(width - Len(text), " ")
Else
PadRight = Left(text, width)
End If
End Function