Default Folder with OpenDialog

lopiteaux

Board Regular
Joined
Jun 8, 2011
Messages
77
Hi,
I have an OpenDialog that needs to be executed prior to executing a bit of code. I currently have it down as follows:

Code:
    Dim Filter As String, Title As String
    Dim FilterIndex As Integer
    Dim Filename As Variant
'WF1.1
'OpenDialog Parameters:
    'Set File Filters (File Types that can be opened/viewed via the OpenDialog)
    Filter = "Excel Files (*.xls),*.xls," & _
        "Comma Separated Files (*.csv),*.csv," & _
        "Text Files (*.txt),*.txt," & _
        "All Files (*.*),*.*"
    FilterIndex = 2 ' Defaults the File Filter to *.csv.
    Title = "Please Select the Relevant Report:" 'OpenDialog caption.
    With Application
        Filename = .GetOpenFilename(Filter, FilterIndex, Title) 'Creates the OpenDialog box with the defined criteria.
        ChDir (.DefaultFilePath) 'Resets the Default Pathname.
    End With
'ErrorHandler - Exits the SubRoutine if no File is selected:
    If Filename = False Then
        msgtitle = "DefaultTitle"
        msgbody = "No file was selected."
        msgbody = msgbody & vbNewLine & vbNewLine & "Please try again."
        msgconfig = vbOKOnly + vbCritical
        msgans = MsgBox(msgbody, msgconfig, msgtitle)
        Exit Sub
    End If
    
'Open the specified file:
    Workbooks.Open Filename

This works great, except for one thing - the default pathname. I am working on a network, and cannot get it to revert to a default pathname, which is written in the following format:

Code:
[/URL][URL="file://abcde12345/Path/Path/Path"]\\abcde12345\Path\Path\Path[/URL][URL="file://\\abcde12345\Path\Path\Path"]


Any thoughts?

C
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Have you tried using ChDrive and ChDir to navigate to that path before showing the dialog?

Hi Andrew, thanks for getting back to me. I have indeed yes, and have found that this works when specifying the actual mapped pathname (e.g. ChDrive ("Q")), but not when wanting to specify the network path ("\\abcde12345"), which I would have to do in order to make the code usable for everyone in my team.

Any thoughts?

C.
 
Upvote 0
Code for Excel 64-bit compatibility:
Rich (BB code):

#If Win64 Then
  Private Declare PtrSafe Function SetCurDir Lib "kernel32" Alias "SetCurrentDirectoryA" (ByVal lpPathName As String) As Long
#Else
  Private Declare Function SetCurDir Lib "kernel32" Alias "SetCurrentDirectoryA" (ByVal lpPathName As String) As Long
#End If

Sub Test()
  'SetCurDir "\\Server1\Common\Tmp"
  SetCurDir ThisWorkbook.Path
End Sub
 
Upvote 0

Forum statistics

Threads
1,225,073
Messages
6,182,704
Members
453,132
Latest member
nsnodgrass73

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