Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default Open file with open dialog box

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
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 37
Default Open file with open dialog box

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

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default Open file with open dialog box

James... Thank you very much...

last time I put

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

Jafery

" wrote:

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


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 37
Default Open file with open dialog box

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

Regards,

James

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10
Default Open file with open dialog box

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

" wrote:

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




  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Open file with open dialog box

When I tested your code, it didn't cause a loop. But I suspect that it depends
on what's in that really long script.

Are you trying to open a few files in the same folder (consolfilemulti kind of
sounds like it)?

You could use something like:

Option Explicit
Sub testme()

Dim myFileNames As Variant
Dim iCtr As Long
Dim wkbk As Workbook

myFileNames = Application.GetOpenFilename _
(filefilter:="Excel Files, *.xls", MultiSelect:=True)

If IsArray(myFileNames) Then
For iCtr = LBound(myFileNames) To UBound(myFileNames)
Set wkbk = Workbooks.Open(Filename:=myFileNames(iCtr))
'do what you want here
wkbk.Close savechanges:=False
Next iCtr
End If

End Sub

If you're allowing the user to chose a file (any folder) and continue until they
cancel, I'd use something like:

Option Explicit
Sub ConsolFileMulti()
Dim myFileName As Variant
Do
myFileName = GetFileNameMulti
If myFileName = False Then
Exit Do
Else
Application.Workbooks.Open myFileName
'call the long loop here
End If
Loop
End Sub
Function GetFileNameMulti() As Variant
Dim myCurFolder As String
myCurFolder = CurDir
GetFileNameMulti = Application.GetOpenFilename
ChDrive myCurFolder
ChDir myCurFolder
End Function

Using a line like "End" (not "End Sub" or "End Function" or "End if") is not a
nice way to exit a routine. If you have public variables, you'll lose their
contents.



NoelH wrote:

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

" wrote:

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



--

Dave Peterson
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Open Dialog Box to return Selected File Path and Not Open it. DMS Excel Programming 4 January 26th 10 12:46 PM
Open File Dialog Box Tha BeatMaker[_8_] Excel Programming 1 July 2nd 05 11:34 PM
File open dialog Vaughan Excel Discussion (Misc queries) 0 May 12th 05 08:50 AM
File Open Dialog scantor145[_3_] Excel Programming 1 September 29th 04 12:04 AM
File open dialog Jan Kronsell[_2_] Excel Programming 1 September 5th 03 08:56 AM


All times are GMT +1. The time now is 01:48 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"