GetOpenFilename to Default Directory

Excel Jr

New Member
Joined
Aug 17, 2018
Messages
26
Hello,

I am trying to update my code to open the dialogue box to my desktop. Not quite sure how to do this with the GetOpenFileName method... I did some googling, and tried the following, but no luck. Any advice appreciated.

VBA Code:
    Dim reconFile As String

    ChDrive "C:\"
    ChDir "C:\Users\Public\Desktop\"

    reconFile = XL.Application.GetOpenFilename(FileFilter:="Microsoft Excel Workbooks (*.xls;*.xlsx;*.xlsm),*.xls;*.xlsx;*.xlsm", _
    Title:="Select reconciliation file you wish to update")
 
@johnnyL, your assumption is wrong.
The Outlook.Application object doesn't have a member with the name FileDialog (so your code will not work).
That's why I wrapped it in a separate function and did qualify al members explicitly.
The additional BuildProperFolderPath function ensures that the string involved ends with a (just one!) backslash character.
That way one doesn't have to check each time whether a folder string ends with a "\" or not when composing a path to a certain file.
In this case it's used to end up in that folder without having the file name: input box of the dialog populated with the name of the desired folder (which imo makes no sense ...).
 
Upvote 0

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
@GWteB You said my code wouldn't work and that leaves me confused. Nothing new there. :rolleyes:

You suggested to the OP to add a couple of functions and then replace the following:
VBA Code:
    reconFile = XL.Application.GetOpenFilename(FileFilter:="Microsoft Excel Workbooks (*.xls;*.xlsx;*.xlsm),*.xls;*.xlsx;*.xlsm", _
    Title:="Select reconciliation file you wish to update")

with:
VBA Code:
tempSaveLocation = "C:\Users\" & Environ("UserName") & "\Downloads\"
reconFile = PickReconciliationFile(tempSaveLocation, XL)

Which results with a full file path to the file chosen saved into the variable 'reconFile'.


I suggest replacing:
VBA Code:
    reconFile = XL.Application.GetOpenFilename(FileFilter:="Microsoft Excel Workbooks (*.xls;*.xlsx;*.xlsm),*.xls;*.xlsx;*.xlsm", _
    Title:="Select reconciliation file you wish to update")
    
    tempSaveLocation = "C:\Users\" & Environ("UserName") & "\Downloads\"

with:
VBA Code:
    With Application.FileDialog(msoFileDialogFilePicker)
        .Filters.Clear
        .Filters.Add "Excel Files", "*.xls; *.xlsx; *.xlsm", 1
        .Title = "Select reconciliation file you wish to update"
        .AllowMultiSelect = False
''        .InitialFileName = "C:\Users\" & Environ("UserName") & "\Desktop"
        .InitialFileName = "C:\Users\" & Environ("UserName") & "\Downloads"
'
        If .Show = True Then reconFile = .SelectedItems.Item(1)
    End With

Which results with a full file path to the file chosen saved into the variable 'reconFile'.


So can you please explain what I am missing? It seems to yield the same result.
 
Upvote 0
@johnnyL, your assumption is wrong.
The Outlook.Application object doesn't have a member with the name FileDialog (so your code will not work).
The OP is running his code in Outlook VBA, that's what you are missing ;)
 
Upvote 0
Dang it!
I don't see where that was stated. Thank you for the clarification though @GWteB!
 
Upvote 0

Forum statistics

Threads
1,214,945
Messages
6,122,397
Members
449,081
Latest member
JAMES KECULAH

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