Link to home
Start Free TrialLog in
Avatar of Samuel-AMC
Samuel-AMC

asked on

VB6 Align text in text documents

Hello experts,

I'm looking for a way on how to align text to the left
in text documents, Something similar to this example:

FROM THIS:

AKDKGDKGJDIVDVO
FJFJGHKJSKS
SKSKS
SJSKKFSFSFLK

TO THIS:

SKSKS
FJFJGHKJSKS
SJSKKFSFSFLK
AKDKGDKGJDIVDVO

I don't know if i am asking in the wrong topic, or what, but the true is
that i don't know if i can make this myself using VB or if there is some
kind of tool that could allow me to do this.

Thanks in advance.
Avatar of Martin Liss
Martin Liss
Flag of United States of America image

Your example in both cases is aligned left. Are you talking about sorting by the length of the data? BTW where is the data? In a textbox, a Word doc or???
Avatar of Samuel-AMC
Samuel-AMC

ASKER

Hi Martin,

I hope i can answer you in the correct way, but i'll try.
I always work with text documents.txt in which the content is always scrambled,
And at the same time i always wanted to find a way that in some sort of way i can
organise all the content of the document in a range way or some correct order...
something similar to this, i meant, suppose i have a text.txt document i would like
to process so i could add to the tool and then process what ever is in the document
to something like this:

FROM THIS:

000000000000000000
00000
0000000000000
000
00
0000000000
000000000000000000000000

TO THIS:

00
000
00000
0000000000
0000000000000
000000000000000000
000000000000000000000000

Thanks Martin
Where do you do this? Word, Excel, VB6, VB.Net?
If there was a way, i was thinking to do it in Visual Basic which is the language i know the most.
But the true is that i don't have any example or code yet, and about the document format,
it have to be notepad, i meant .txt document format.
Do you really have classic VB6?
...or are you working in VB thru MS Office?
...or VBScript (.vbs)?
...or VB.Net (Visual Studio Express maybe)?
Yes, i do have Visual Basic Classic, i meant, VB6.
I can't get used to the new Visual Basic 2005, 2008, 2011, etc.
ASKER CERTIFIED SOLUTION
Avatar of Martin Liss
Martin Liss
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
WoooooW Martin!!! that was so nice!!! Thank You! Very Much!!!
WooW!!! man, this is so nice ^_^
I knew there was i way, but did not know how to accomplish something like this,
Thank You Very Much for all your help and support Martin :-)
You're welcome and I'm glad I was able to help.

My profile contains links to some articles I've written that may interest you.
Marty - MVP 2009 to 2012
Thanks Marty, i am always after those projects :-)
I will check them, Thank You Again.
Here's one with QuickSort instead of BubbleSort:
Option Explicit

Private Sub Command1_Click()
    Dim fileName As String
    fileName = "C:\Users\Mike\Documents\someFile.txt"
    
    Dim lines() As String
    lines = Split(GetFileContents(fileName), vbCrLf)
    
    QuicksortByLineLength lines, LBound(lines), UBound(lines)
    
    WriteFileLines fileName, lines
End Sub

Private Function GetFileContents(ByVal fileName As String) As String
    Dim ff As Integer
    Dim entireFile As String
    
    If Dir(fileName) <> "" Then
        ff = FreeFile
        Open fileName For Binary Access Read As #ff
        GetFileContents = Input(LOF(ff), ff)
        Close #ff
    Else
        MsgBox fileName, vbExclamation, "File Not Found"
    End If
End Function

Private Sub WriteFileLines(ByVal fileName As String, ByVal lines As Variant)
    Dim ff As Integer
    ff = FreeFile
    Open fileName For Binary Access Write As #ff
    Put #ff, , Join(lines, vbCrLf)
    Close #ff
End Sub

Private Sub QuicksortByLineLength(ByRef list As Variant, ByVal min As Integer, ByVal max As Integer)
    Static randomized As Boolean
    If Not randomized Then
        randomized = True
        Randomize
    End If
    
    Dim med_value As Variant
    Dim hi As Integer
    Dim lo As Integer
    Dim i As Integer
    
    ' If the list has no more than 1 element, it's sorted.
    If min >= max Then Exit Sub

    ' Pick a dividing item.
    i = Int((max - min + 1) * Rnd + min)
    med_value = list(i)
   
    ' Swap it to the front so we can find it easily.
    list(i) = list(min)

    ' Move the items smaller than this into the left
    ' half of the list. Move the others into the right.
    lo = min
    hi = max
    Do
        ' Look down from hi for a value < med_value.
        Do While Len(list(hi)) >= Len(med_value)
            hi = hi - 1
            If hi <= lo Then Exit Do
        Loop
        If hi <= lo Then
            list(lo) = med_value
            Exit Do
        End If

        ' Swap the lo and hi values.
        list(lo) = list(hi)
        
        ' Look up from lo for a value >= med_value.
        lo = lo + 1
        Do While Len(list(lo)) < Len(med_value)
            lo = lo + 1
            If lo >= hi Then Exit Do
        Loop
        If lo >= hi Then
            lo = hi
            list(hi) = med_value
            Exit Do
        End If

        ' Swap the lo and hi values.
        list(hi) = list(lo)
    Loop

    ' Sort the two sublists
    QuicksortByLineLength list, min, lo - 1
    QuicksortByLineLength list, lo + 1, max
End Sub

Open in new window

BTW if you do find any of my articles helpful, there's a button you can click that says so:)
That was a good one Martin :-) don't worry i like to read a lot :-)

Idle_Mind, thank you for your code.
No problem dude.
*I started it, but then had to get the kids into bed.  Martin had already answered when I came back, but I figured I'd post it anyways.  =)
Thank You Idle Mind, I really appreciate your contribution,
You have a pleasant night you and your children, thank you again.