Private Function FormatarString(strng, tamanho As Integer)
If Len(strng) > tamanho Then
FormatarString = Left(strng, tamanho)
Else
FormatarString = strng & String(tamanho - Len(strng), " ")
End If
End Function
Debug.Print FormatarString("The quick brown fox jumps over the lazy dog", 20) & ";" & vbCrLf & _
FormatarString("Teste", 20) & ";" & vbCrLf & _
FormatarString("Mais um teste", 20) & "."
FormatarString = strng & String(tamanho - Len(strng), " ")
FormatarString = String(tamanho - Len(strng), " ") & strng
Open "ficheiro.txt" For Output As #1 'Abre o ficheiro para escrita. Se ele existir, apaga-o.
Print #1, "abc" 'escreve isto
Print #1, "def" 'e isto
Close #1 'Fecha o ficheiro
Open "ficheiro.txt" For Append As #1 'Abre o ficheiro para escrita. Se ele existir, acrescenta tudo isto no fim
Print #1, "ghi" 'escreve mais isto
Close #1 'Volta a fechar
Open "ficheiro.txt" For Input As #1 'Abre o ficheiro para leitura.
Do Until EOF(1)
Line Input #1, buffer 'Extrai cada linha do ficheiro, uma a uma, até chegar ao EOF - End Of File
MsgBox buffer 'Mostra cada linha numa MsgBox
Loop
Close #1 'E fecha o ficheiro de novo