You have undeclared variables in your code and a totally unnecessary use of On Error Resume Next
where you attempt to get the Ubound(filePath)
before you instantiate filePath
as an array.
GET RID OF THESE LINES:
' On Error Resume Next
' num = UBound(filepath)
' On Error Goto 0 )
You should declare your variables properly. Your user form must contain textbox controls named like TextBox1
, TextBox2
, ... TextBox8
.
Dim filePath() as Variant
Dim myFile as Variant
Dim num As Integer: num = 1
'### Redim this array as a base-1 array
Redim Preserve filepath(num to .SelectedItems.Count)
'### I made some revisions here:
For Each file In .SelectedItems
Set objFile = fso.GetFile(file)
filepath(num) = file 'Store a string, rather than an object.
Me.Controls("TextBox" & num).Value = file
num = num + 1
Next
TextBox14.Text = num & (" - Ficheiro(s) Adicionado(s)")
Code is working and displays 6 different selections in an example file that I created: