Option Explicit
' =================================================================
' Text file contents must have line breaks in certain spots so it
' can be read into the listbox correctly. It takes some trial and
' error to figure on the formatting correctly so the contents can
' all be read.
' =================================================================
Private Sub UserForm_Initialize()
' Clear the list box and caption
With Me
.lstText.Clear
.lblFilePath.Caption = Empty
End With
End Sub
Private Sub cmdRun_Click()
' Text files name
Const szFileName As String = "ReadMe.txt"
' Clear the list box and caption
With Me
.lstText.Clear
.lblFilePath.Caption = Empty
End With
' =======================================================
' Variables for creating a path that the
' text file is expected to be in
Dim szThisPath As String
szThisPath = ThisWorkbook.Path
Dim szPathSep As String
szPathSep = Application.PathSeparator
Dim szValidPath As String
szValidPath = szThisPath & szPathSep & szFileName
' =======================================================
' Place the files name and path location into a label, just
' so you remember where your file came from. Relevant if your
' going to incorporate a browse for file routine
Me.lblFilePath.Caption = "Reading file from: " & szValidPath
' We need an Error handler just in case the file we hard-coded is
' missing, renamed, etc...
On Error Goto ErrHandler
' =======================================================
' Read each line from the text file, adding it to the
' list box as an item
Dim lFile As Long
Dim szLine As String
lFile = FreeFile()
Open szValidPath For Input As lFile
While Not EOF(lFile)
Line Input #lFile, szLine
Me.lstText.AddItem szLine
Wend
Close lFile
' =======================================================
Exit Sub
ErrHandler:
Me.lblFilePath.Caption = Empty
MsgBox Err.Description
End Sub
Private Sub lstText_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
' De-select current items
Dim i As Long
For i = 0 To Me.lstText.ListCount - 1
Me.lstText.Selected(i) = False
Next i
End Sub
Private Sub cmdClose_Click()
' Close the form
Unload Me
End Sub
'In standard module
Sub ShowMyForm()
frmGetTxt.Show
End Sub
|