Browse directory

Agent Orange

New Member
Joined
Sep 8, 2002
Messages
2
I have a browse button and I want to set my default directory, in which when i press the button it goes to that directory rather than mydocuments or some crap...Any help would be great thanks in advance.

Here is the code I am using for the browse...
[
Dim Filter As String, Caption As String, SelectedFile As Variant

Filter = "Excel (*.xls),*.xls"
Caption = "Please Select SpreadSheet"

SelectedFile = Application.GetOpenFilename(Filter, , Caption)
If SelectedFile = False Then Exit Sub

Me.destination = SelectedFile

Set SelectedFile = Nothing
]
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
On 2002-09-09 14:33, Agent Orange wrote:
I have a browse button and I want to set my default directory, in which when i press the button it goes to that directory rather than mydocuments or some crap...Any help would be great thanks in advance.

Here is the code I am using for the browse...
[
Dim Filter As String, Caption As String, SelectedFile As Variant

Filter = "Excel (*.xls),*.xls"
Caption = "Please Select SpreadSheet"

SelectedFile = Application.GetOpenFilename(Filter, , Caption)
If SelectedFile = False Then Exit Sub

Me.destination = SelectedFile

Set SelectedFile = Nothing
]

You can use the ChDrive and ChDir statements. This will not change the default directory for Excel, but it will affect the path that Excel will default to for the rest of the session (so if someone runs the macro then goes to File-Open, it will still use your path) - unless you change it back in your code (which I have done below). Something like this:<pre><font color='#000000'><font color='#000080'>Sub</font> OpenMyFile()<font color='#000080'>Dim</font> Filter<font color='#000080'>As</font> String, Caption<font color='#000080'>As</font> String, SelectedFile<font color='#000080'>As</font><font color='#000080'>Variant</font><font color='#000080'>Dim</font> strCurrentDrive<font color='#000080'>As</font> String, strCurrentPath<font color='#000080'>As</font><font color='#000080'>String</font>

strCurrentPath = CurDir$
strCurrentDrive = Left(strCurrentPath, 1)

ChDrive "d"
ChDir "d:data"

Filter = "Excel (*.xls),*.xls"
Caption = "Please Select SpreadSheet"

SelectedFile = Application.GetOpenFilename(Filter, , Caption)<font color='#000080'>If</font> SelectedFile = False<font color='#000080'>Then</font><font color='#000080'>GoTo</font> ExitHere

Me.Destination = SelectedFile<font color='#000080'>Set</font> SelectedFile = Nothing

ExitHere:
ChDrive strCurrentDrive
ChDir strCurrentPath<font color='#000080'>Exit</font><font color='#000080'>Sub</font><font color='#000080'>End</font><font color='#000080'>Sub</font></font></pre>

Hope this helps,

Russell
This message was edited by Russell Hauf on 2002-09-09 16:12
 
Upvote 0

Forum statistics

Threads
1,215,429
Messages
6,124,839
Members
449,193
Latest member
MikeVol

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top