Set default file browse location VBA

2020-04-17 07:23发布

I have the following VBA code that browses for a file name within MS ACCESS form:

Private Sub Command64_Click()
Dim dialog As FileDialog
Dim filePath As String
Dim fileName As String

Set dialog = Application.FileDialog(msoFileDialogFilePicker)

 With dialog
.AllowMultiSelect = False

.Show
 If (.SelectedItems.Count = 0) Then
 Else
    filePath = .SelectedItems.Item(1)
    fileName = Right$(filePath, Len(filePath) - InStrRev(filePath, "\"))
    Me.Thumbnail = fileName
 End If
End With
End Sub

I'd like to set a default location for where the file browser opens up to. Is this possible?

标签: access-vba
2条回答
The star\"
2楼-- · 2020-04-17 07:37

You can make use of the InitialFileName property of the FileDialog method.

Private Sub Command64_Click()
    Dim dialog As FileDialog
    Dim filePath As String
    Dim fileName As String

    Set dialog = Application.FileDialog(msoFileDialogFilePicker)

    With dialog
        .AllowMultiSelect = False
        .InitialFileName = "C:\yourFolderNameHere\"
        .Show
        If .SelectedItems.Count <> 0 Then
            filePath = .SelectedItems.Item(1)
            fileName = Right$(filePath, Len(filePath) - InStrRev(filePath, "\"))
            Me.Thumbnail = fileName
        End If
    End With
End Sub
查看更多
▲ chillily
3楼-- · 2020-04-17 07:39

Yes, try this:

Private Sub Command64_Click()
Dim dialog As FileDialog
Dim filePath As String
Dim fileName As String

Dim directory As String
' Set a default location
directory = "C:\"

Set dialog = Application.FileDialog(msoFileDialogFilePicker)

 With dialog
.AllowMultiSelect = False
.InitialFileName = directory
.Show


If (.SelectedItems.Count = 0) Then
 Else
    filePath = .SelectedItems.Item(1)
    fileName = Right$(filePath, Len(filePath) - InStrRev(filePath, "\"))
    Me.Thumbnail = fileName
 End If
End With
End Sub
查看更多
登录 后发表回答