Set default file browse location VBA

后端 未结 2 1426
天命终不由人
天命终不由人 2021-01-22 21:30

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
D         


        
相关标签:
2条回答
  • 2021-01-22 22:08

    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
    
    0 讨论(0)
  • 2021-01-22 22:16

    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
    
    0 讨论(0)
提交回复
热议问题