Excel VBA to open file - how to define default folder

Part of the solution was the following function to open the file. How and where can I set the default folder for opening and also part of the file name (for example xdfaerew*.xls)?

Function FunctionGetFileName(FullPath As String) As String

Dim StrFind As String
Dim iCount As Integer
    Do Until Left(StrFind, 1) = "\"

        iCount = iCount + 1

        StrFind = Right(FullPath, iCount)

            If iCount = Len(FullPath) Then Exit Do

    Loop

    

    FunctionGetFileName = Right(StrFind, Len(StrFind) - 1)

    

End Function

Open in new window


the function is called from the code as follows:

SourceWb = Application.GetOpenFilename 'Allows for user to select file
Workbooks.Open SourceWb 'Open source workbook
SourceWb = FunctionGetFileName(CStr(SourceWb))

Open in new window

BrdgBldrAsked:
Who is Participating?
 
m4trixConnect With a Mentor Commented:
Ah, ok, I gotcha.

The first part is easy:
ChDir "c:\data\ee"
SourceWb = Application.GetOpenFilename("Excel Files (*.xls), *.xls")

Open in new window


That will pop up an "open file" dialog in c:\data\ee\ and show ONLY *.xls files

The second part is not as easy. There's no "out of the box" way to show only files starting with "mountain" and ending with ".xls". However, we can have the macro automatically type some letters in which will accomplish that in a roundabout way. Consider the following completed function:
 
Sub OpenFileFromDefaultDirectory()
    ChDir "c:\data\ee"
    Application.SendKeys "mountain*{RETURN}"
    SourceWb = Application.GetOpenFilename("Excel Files (*.xls), *.xls") 'Allows for user to select file
    Workbooks.Open SourceWb 'Open source workbook
End Sub

Open in new window


It should do as you ask. However, the "SendKeys" line is a little bit clunky.
0
 
m4trixCommented:
Is this what you're looking for?

SourceWb = Application.GetOpenFilename 'Allows for user to select file
    Workbooks.Open SourceWb 'Open source workbook
    SourceWb = Mid(SourceWb, InStrRev(SourceWb, "\") + 1)

Open in new window

0
 
m4trixCommented:
I should have clarified in the previous post:
If you select the file "C:\temp\myworkbook.xlsx"
SourceWb becomes "myworkbook.xlsx" after line 3.

I'm not sure what you're looking for in terms of getting the default folder... can you elaborate?
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
m4trixCommented:
Sorry to make another post, but it just occured to me another option for line 3 is simply the following:
SourceWb = ActiveWorkbook.Name
0
 
BrdgBldrAuthor Commented:
Thanks.

So do you think I don't even need this function?

I would like that when the file open dialogue opens, it is opening in the folder c:\data\ee\  and only showing files for example that start with banana and are xls files so mountain*.xls
so that I could set these defaults in the code
0
 
m4trixCommented:
PS - the above macro can ONLY be run from the main Excel window - via a button or via the macros dialog box. If you run it from the VB Editor it will just type "mountain*" wherever your cursor is on the screen :)
0
 
BrdgBldrAuthor Commented:
ok. that pretty makes it. it even works when starting out of the VBA editor ;-)

last question: if a click on cancel in the file open dialogue... how can I handle this error?

On Error Exit Sub or smthing like this?
0
 
BrdgBldrAuthor Commented:
ok... I see what you mean... with F8 in break mode it doesn't work in the VBA editor :o)
0
 
m4trixConnect With a Mentor Commented:
Yeah, break mode is what I meant :)

As for the cancel, you could do something like this maybe?
Sub OpenFileFromDefaultDirectory()
    ChDir "c:\data\ee"
    Application.SendKeys "mountain*{RETURN}"
    SourceWb = Application.GetOpenFilename("Excel Files (*.xls), *.xls") 'Allows for user to select file
    If SourceWb = False Then
        MsgBox "You didn't select a file!"
    Else
        Workbooks.Open SourceWb 'Open source workbook\
    End If
End Sub

Open in new window


If you don't want any popup at all, then yes, you could Exit Sub
0
 
BrdgBldrAuthor Commented:
many thanks!   :o)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.