Hi guys,
My code is written in outlook vba and I am trying to figure out the issue with storing different data each time in the listbox based on the relevant option selected on the form.
So, If we need to update listbox based on the 'option' selected in a frame , I have written a sub as below. The frame has 3 options 'Pre Completion','Post Completion' and 'Services' . So I have created 3 different text files for those options and stored them on the network.
It gives 'Subscript out of range' error message in the below highlighted line of code. Can you please figure out what's going wrong. Do we need to clear Array each time ?
My code is written in outlook vba and I am trying to figure out the issue with storing different data each time in the listbox based on the relevant option selected on the form.
So, If we need to update listbox based on the 'option' selected in a frame , I have written a sub as below. The frame has 3 options 'Pre Completion','Post Completion' and 'Services' . So I have created 3 different text files for those options and stored them on the network.
It gives 'Subscript out of range' error message in the below highlighted line of code. Can you please figure out what's going wrong. Do we need to clear Array each time ?
Code:
Private Sub PopulateListbox()
Dim fn As String, ff As Integer, txt As String
If Me.Option1 = True Then
fn = "C:\Users\Desktop\Outlook Files\Pre-Completion.txt" '< --- .txt file path
ElseIf Me.Option2 = True Then
fn = "C:\Users\Desktop\Outlook Files\Post-Completion.txt" '< --- .txt file path
ElseIf Me.Option3 = True Then
fn = "C:\Users\Desktop\Outlook Files\Mortgage-Services.txt" '< --- .txt file path
End If
txt = Space(FileLen(fn))
ff = FreeFile
Open fn For Binary As #ff
Get #ff, , txt
Close #ff
Dim myArray() As String
Dim i As Integer
myArray = Split(txt, vbCrLf)
'Use .List method to populate listbox.
With ListBox1
.ColumnCount = 2
For i = 0 To UBound(myArray)
.AddItem
[COLOR="Red"].List(i, 0) = Split(myArray(i), ",")(0)[/COLOR]
.List(i, 1) = Split(myArray(i), ",")(1)
Next i
.ColumnWidths = "0pt;" & .Width - 4
End With
End Sub
Last edited: