设置默认文件浏览位置VBA

时间:2014-07-29 13:54:33

标签: access-vba

我有以下VBA代码浏览MS ACCESS表单中的文件名:

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

我想为文件浏览器打开的位置设置默认位置。这可能吗?

2 个答案:

答案 0 :(得分:2)

您可以使用FileDialog方法的InitialFileName属性。

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

答案 1 :(得分:0)

是的,试试这个:

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