Search code examples
filetextvb6

Generating a numbered list with VB6


I'm a novice using VB6 to try and create a basic text file program. Basically in my work I create lots of text files with headings and then variable data so to save me manually typing the document with each heading every time I created a VB6 program which would automatically add the headings, I would enter the data and it would output it all as a text file. I got most of it done, but there is one part I can't do.

One part of the text file is a numbered list with a dot after it. EG:

HEADING
01. CHEESE
02. CHOCOLATE
03. BREAD

and so on.

The list is different in each file and a different length, so could be 4 items, could be 20. At the moment I've just got a plain TextBox and I manaully enter the data as above each time, with the numbers. What I would like to do is have the number, dot and space automatically created in front of the list. So I could just enter the list as is

CHEESE
CHOCOLATE
BREAD

and when I generate the text file the list is automatically numbered. Is that possible with VB6? I know it seems like a small detail but I create hundreds of these files and the less effort I can make it for each one the better.

Private Sub create_Click()
Dim fso
Dim file As String
file = "C:\Textfile.txt"
Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FileExists(file) Then
    fso.DeleteFile file, True
End If
Const ForAppending = 8
Set filesys = CreateObject("Scripting.FileSystemObject")
Set filetxt = filesys.OpenTextFile("C:\Textfile.txt", ForAppending, True)
With filetxt
.writeline (txtArtist & vbNewLine)
.writeline ("SOURCE" & " (" & Combo1 & " #" & txtsource & ")" & ":")
.writeline (txtequip & vbNewLine)
.writeline (Combo2 & ":")
.writeline (txttransfer & vbNewLine)
.writeline ("GENERATION:")
.writeline (txtgen & vbNewLine)
.writeline ("LENGTH:")
.writeline (txtlength & vbNewLine)
.writeline ("NOTES:")
.writeline (txtnotes & vbNewLine)
.writeline ("TRACKS:")
.writeline (txttracks & vbNewLine)
.writeline ("MD5 FINGERPRINTS:")
.writeline (txtmd5 & vbNewLine)
.writeline ("TRANSFERRED BY:")
.writeline (txttransferby & vbNewLine)
.writeline ("**PLEASE DO NOT ENCODE TO LOSSY FORMATS OR SELL!**")
.Close
End With
Shell "notepad.exe C:\Textfile.txt", vbNormalFocus
End Sub

Solution

  • You can:

    Text1.Text = TextToNumberedList(Text1.Text)
    
    Function TextToNumberedList(strData As String) As String
        Dim arr()  As String
        Dim i      As Long
        arr = Split(strData, vbCrLf)
        For i = 0 To UBound(arr)
            arr(i) = Format$(i + 1, "00") & ". " & arr(i)
        Next
    
        TextToNumberedList = Join(arr, vbCrLf)
    End Function