Open file with open dialog box

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi
I let user to open his/her own file with this VBA......

Sub ConsolFile()
Application.Workbooks.Open (GetFileName)
End Sub

Function GetFileName()
'Get a FileName (may change current drive/directory.)
Filename = Application.GetOpenFilename
'Get the current directory and drive
TempDir = CurDir()
'Switch back to original drive
ChDrive Mid(TempDir, 1, 1)
'Switch back to the orginal directory
ChDir TempDir
'Return the filename
GetFileName = Filename
End Function

It doesn't work 'cause it return false when user cancel....How can I fix
this problem?

Thanks
Jafery
 
Hi Jafery,

Somewhere with in either your function/module you just need to say if
Getfilename = false then...

With in your module you could write:

If Getfilename = False then
Msgbox "No File was selected, the macro will now end"
End
Else
Application.Workbooks.Open (GetFileName)
End if

Regards,

James
 
James... Thank you very much...

last time I put

getfilename ="False" ...cause I think the function return character....
and it doesn't work

Jafery
 
Yeah, it was returning a boolean result rather than a string hence no
"" were required.

Regards,

James
 
Hi James

I have been trying this little script. It runs through fine but then wishes
to restart from the beginning but then end once it has opened the file.

This is what I have. Except for my very long script.

Sub ConsolFileMulti()
Application.Workbooks.Open (GetFileNameMulti)
End Sub

Function GetFileNameMulti()
'Get a FileName (may change current drive/directory.)
Filename = Application.GetOpenFilename
'Get the current directory and drive
TempDir = CurDir()
'Switch back to original drive
ChDrive Mid(TempDir, 1, 1)
'Switch back to the original directory
ChDir TempDir
'Return the filename
GetFileNameMulti = Filename
If GetFileNameMulti = False Then
MsgBox "No File was selected, the macro will now end"
End
Else
Application.Workbooks.Open (GetFileNameMulti)

End If

' the start of a really long script


‘ end of the really long script

End Function

What am I doing to cause the loop.

TIA Regards Noel
 

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

Back
Top