Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Prob. with returning an Array & GetOpenFilename 2

Status
Not open for further replies.

Gallowgate

Technical User
Mar 4, 2003
14
GB
hi All

Firstly, I'm a beginner with VB. Got the following out of a book and works when I select some files, however, when I choose Cancel in the Openfile Dlg, it returns msg 'Error 13: Type mismatch' on the FOR loop. How can I check if the array is empty(ie no files selected or cancel was pressed). In the debugger, fname equals 'false' when cancel is pressed, why is it false?

Code:
Const iTitle = "Pick file"
Const Filterlist = _
 "Word(*.doc), *.doc, Excel(*.xls), *.xls"
Dim i As Integer

With Application
    Fname = .GetOpenFilename(Title:=iTitle, _
    filefilter:=Filterlist, _
    FilterIndex:=2, _
    MultiSelect:=True)
End With
 
For i = LBound(Fname) To UBound(Fname)
   tbfilename.Text = tbfilename.Text & Fname(i) & "; "
Next i

thanks for any help
Martin
 
Thanks for the reply,

I tried that this morning and when I put the if statement to catch the cancel error, it then fails on the OK button when I have selected files. Error is Type Mismatch.


Thanks again.

Martin
 
hi Skip
Tried to declare it as a variant (remember I'm a beginner here, so please be patient with me).

This is the code as it stands and I get a Type mismatch on the IF statement. Could you give me the corrected version, please, as I'm confused with it?

Code:
Dim Fname As Variant
Dim i As Integer


With Application
    Fname = .GetOpenFilename(Title:=iTitle, _
    filefilter:=Filterlist, _
    FilterIndex:=2, _
    MultiSelect:=True)
End With
 
If Fname <> False Then
 For i = LBound(Fname) To UBound(Fname)
   tbfilename.Text = tbfilename.Text & Fname(i) & &quot;; &quot;
Next i
End If
 
Herewith whole thing...


Code:
 Const iTitle = &quot;Pick file&quot;
Const Filterlist = _
 &quot;Word(*.doc), *.doc, Excel(*.xls), *.xls&quot;
Dim Fname As Variant
Dim i As Integer


With Application
    Fname = .GetOpenFilename(Title:=iTitle, _
    filefilter:=Filterlist, _
    FilterIndex:=2, _
    MultiSelect:=True)
End With

 Rem Label1.Caption = Fname

 Rem if (fname is ) = false then
 
If Fname <> False Then
 For i = LBound(Fname) To UBound(Fname)
   tbfilename.Text = tbfilename.Text & Fname(i) & &quot;; &quot;
Next i
End If

End Sub
 
hi

(Firstly, to avoid any confusion, I've been working with Gallowgate on this one.)

The IsError started to give us problems(not sure why) and wasn't trapping the pressing of Cancel.

We have since discovered that because Fname is a variant, Excel will return the result &quot;False&quot; converted to a string if Cancel is pressed - why it can't convert it to Boolean(False), I don't know . The following now works better [phew]:-

if Fname = &quot;False&quot; then ...

lou

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top