GetOpenFileName + Type Mismatch error

D

David

Hi Excellers

I have read several posts on the Cancel event using the
GetOpenFileName method. What has been posted "seemed" OK for the
examples shown but I am having other difficulties. I get Error 13
"Type Mismatch". The type mismatch occurs when the user selects a
file giving strFile a string path. The problem occurs when the code
goes to the If statement and tries to evaluate strFile. It is
expecting a boolean vaue but gets a string => error!
My code is:
Private Sub cmdStart_Click()
Dim strFile As String

strFile = Application.GetOpenFilename("Excel Files (*.xls), *.xls", ,
"Open strFile.xls file")
If strFile = False Then
'User has cancelled
MsgBox "Data Update Cancelled", vbInformation, "DATA UPDATING"
Exit Sub
End If

MsgBox "All code has worked"

End Sub

'As always, your help is much appreciated.
 
B

Bob Phillips

If you are going to define it as a string test it as a string

Private Sub cmdStart_Click()
Dim strFile As String

strFile = Application.GetOpenFilename( _
"Excel Files (*.xls), *.xls", , "Open strFile.xls file")
If strFile = "False" Then
'User has cancelled
MsgBox "Data Update Cancelled", vbInformation, "DATA UPDATING"
Exit Sub
End If

MsgBox "All code has worked"

End Sub

or else define it as variant

Private Sub cmdStart_Click()
Dim strFile As Variant

strFile = Application.GetOpenFilename( _
"Excel Files (*.xls), *.xls", , "Open strFile.xls file")
If strFile = False Then
'User has cancelled
MsgBox "Data Update Cancelled", vbInformation, "DATA UPDATING"
Exit Sub
End If

MsgBox "All code has worked"

End Sub

--
---
HTH

Bob


(there's no email, no snail mail, but somewhere should be gmail in my addy)
 
P

Peter T

You could fix that in various ways, eg

change
Dim strFile As String
to
Dim strFile As Variant ' also rename the variant to say vFile

or -
leave strFile declared As String
and change
If strFile = False Then
to
If strFile = cstr(False) Then

Regards,
Peter T
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top