Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations dencom on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I set the folder to display using FileDialog? 1

Status
Not open for further replies.

duGly

Technical User
Nov 13, 2006
52
US
I am using VBA with Word 2003. I am using the FileDialog object to return the path and name of a folder that the user selects. How do I set the folder that FileDialog opens to? I have tried ChDrive and ChDir before opening the dialog, but it still opens to my Word's default file folder. Here is my code:
Code:
Private Sub cmdChange_Click()[green]

    ' Change drive and directory.[/green]
    ChDrive g_strFolder
    ChDir g_strFolder[green]

    ' Display FileDialog to set g_strFolder.[/green]
    With Application.FileDialog(msoFileDialogFolderPicker)
        .AllowMultiSelect = False
        .ButtonName = "OK"
        .Title = "Select Project Folder"
        If .Show = 0 Then Exit Sub
        g_strFolder = .SelectedItems(1)
    End With[green]
    
    ' Set caption for lblProjectLocation.[/green]
    Me.lblProjectLocation.Caption = g_strFolder

End Sub
 
Try:

Code:
With Application.FileDialog(msoFileDialogFolderPicker)
        .AllowMultiSelect = False
        .ButtonName = "OK"
        .Title = "Select Project Folder"
        .InitDir="SomePath"
        If .Show = 0 Then Exit Sub
        g_strFolder = .SelectedItems(1)
    End With

I hope this helps.




Ron Repp

If gray hair is a sign of wisdom, then I'm a genius.
 
Thanks, Ron. There is actually not an [blue].InitDir[/blue] property for the FileDialog, but there is an [blue].InitialFileName[/blue]. Since I'm searching for a folder, I used a wildcard to return a child of my desired folder. Here's my code:
Code:
Private Sub cmdChange_Click()[green]

    ' Display FileDialog to set g_strFolder.[/green]
    With Application.FileDialog(msoFileDialogFolderPicker)
        .AllowMultiSelect = False
        .ButtonName = "OK"
        .Title = "Select Project Folder"
        .InitialFileName = g_strFolder & "\*"
        If .Show = 0 Then Exit Sub
        g_strFolder = .SelectedItems(1)
    End With[green]
    
    ' Set caption for lblProjectLocation.[/green]
    Me.lblProjectLocation.Caption = g_strFolder

End Sub
Thanks for pointing me in the right direction.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top