Solved File Dialog not Working (1 Viewer)

RogerCooper

Registered User.
Local time
Today, 07:44
Joined
Jul 30, 2014
Messages
288
I need to have a user select a file for further operations using a normal dialog. I tried this code, but nothing happens, not even an error.

Code:
Dim FileName As String
Dim FD As FileDialog
Set FD = Application.FileDialog(msoFileDialogFilePicker)
FileName = FD.InitialFileName

I have the Microsoft Office 16.0 object library in references. Is there a better way to do this?
 

cheekybuddha

AWF VIP
Local time
Today, 15:44
Joined
Jul 21, 2014
Messages
2,288
You haven't displayed the dialog.

Your code should be more along the lines of:
Code:
Dim FileName As String
Dim FD As FileDialog
Set FD = Application.FileDialog(msoFileDialogFilePicker)
With FD
  .InitialFileName = 'C:\Some\Path'
  .Title = "Choose a file ..."
  If .Show Then         ' <--  This line displays the dialog
    FileName = .SelectedItems(1)
  End If
End With
If Len(FileName) Then
  ' User chose a file
Else
  ' User did not choose a file / cancelled
End If
Set FD = Nothing
 

LarryE

Active member
Local time
Today, 07:44
Joined
Aug 18, 2021
Messages
592
I need to have a user select a file for further operations using a normal dialog. I tried this code, but nothing happens, not even an error.

Code:
Dim FileName As String
Dim FD As FileDialog
Set FD = Application.FileDialog(msoFileDialogFilePicker)
FileName = FD.InitialFileName

I have the Microsoft Office 16.0 object library in references. Is there a better way to do this?
Code:
Dim SelectedFile As Variant
Dim FD As FileDialog
Set FD = Application.FileDialog(msoFileDialogFilePicker)
With FD
    .AllowMultiSelect = False
    .Title = "Select A Backend File"
    .ButtonName = "Select"
    .Filters.Clear
    .Filters.Add "Access Files", "*.accdb", 1
    .FilterIndex = 1
    If .Show = -1 Then 'If user selected a file
        For Each SelectedFile In FD.SelectedItems
            MsgBox SelectedFile
        Next
    End If
End With
I do it this way
 

cheekybuddha

AWF VIP
Local time
Today, 15:44
Joined
Jul 21, 2014
Messages
2,288
@LarryE - why do you loop the FD.SelectItems when you have already set .AllowMultiSelect = False?
 

RogerCooper

Registered User.
Local time
Today, 07:44
Joined
Jul 30, 2014
Messages
288
You haven't displayed the dialog.

Your code should be more along the lines of:
Code:
Dim FileName As String
Dim FD As FileDialog
Set FD = Application.FileDialog(msoFileDialogFilePicker)
With FD
  .InitialFileName = 'C:\Some\Path'
  .Title = "Choose a file ..."
  If .Show Then         ' <--  This line displays the dialog
    FileName = .SelectedItems(1)
  End If
End With
If Len(FileName) Then
  ' User chose a file
Else
  ' User did not choose a file / cancelled
End If
Set FD = Nothing
I needed to remove the line with .InitialFileName, then it worked perfectly. Thank you for the help.
 

LarryE

Active member
Local time
Today, 07:44
Joined
Aug 18, 2021
Messages
592
@LarryE - why do you loop the FD.SelectItems when you have already set .AllowMultiSelect = False?
Just makes it easier to allow multiple files or not by changing just that one line to True or False. If it's True they can select and operate on each file in order. If False, the Next is ignored anyway.
 

Users who are viewing this thread

Top Bottom