Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get a single file name out of a File Dialog object in VBA (for MS Access 2007)?

How do I change my code to get the file name instead of the directory name? openDialog.InitialFilename gives me the directory name.
openDialog.FileName gives me the error "Method or data member not found".

Private Sub btnEditPhoto_Click()
    If (txtImageName > "") Then

        Application.FollowHyperlink txtImageName

    Else
        Dim openDialog As Office.FileDialog

        Set openDialog = Application.FileDialog(msoFileDialogFilePicker)

            openDialog.Filters.Clear
            openDialog.Filters.Add "JPEG Files", "*.jpg"

        Dim pickedFile As Boolean
            pickedFile = openDialog.Show

        If pickedFile Then
                txtImageName.SetFocus
                txtImageName.Text = openDialog.InitialFileName
        End If

    End If

End Sub
like image 867
Christopher Bottoms Avatar asked Jan 28 '10 23:01

Christopher Bottoms


People also ask

How do I open a dialog box in access?

To open the Access Options dialog box, click the File tab on the Backstage view and then click Options, as shown in Figure 2-98. Figure 2-98. Click the File tab on the Backstage view and then click Options to open the Access Options dialog box.

Which dialog box allows the user to select one or more files that can then be opened in Excel using the execute command?

Open dialog box: lets users select one or more files that you can then open in the host application by using the Execute method.

How do I use FileDialog in VBA?

Excel VBA FileDialog – Example #1 Step 1: Go to the Developers tab and click on Visual Basic. Step 2: Open a Module from the Insert menu option as shown below. Step 3: Start the subprocedure to start working on example. Step 4: Declare a variable as Filedialog as shown below.


2 Answers

You want:

OpenDialog.SelectedItems.Item(1)

In place of:

OpenDialog.InitialFileName

As you have not allowed multiselect.


So:

''Reference Microsoft Office x.x Object Library
Dim openDialog As Office.FileDialog

Set openDialog = Application.FileDialog(msoFileDialogFilePicker)
openDialog.Filters.Clear
openDialog.Filters.Add "JPEG Files", "*.jpg"

If openDialog.Show Then
    ''SelectedItems is not zero based

    ''Do not use .Text property in MS Access except
    ''in special cases, then you will not have to set focus
    ''txtImageName.SetFocus

    txtImageName = openDialog.SelectedItems(1)
End If

If AllowMultiSelect is used, you need to iterate through SelectedItems

''Reference Microsoft Office x.x Object Library
Dim openDialog As Office.FileDialog
Dim i As Integer

Set openDialog = Application.FileDialog(msoFileDialogFilePicker)
'Use ctl or shift + click to select more than one file
openDialog.AllowMultiSelect = True
openDialog.Filters.Clear
openDialog.Filters.Add "JPEG Files", "*.jpg"

If openDialog.Show Then
    For i = 1 To openDialog.SelectedItems.Count
        Imagelst = Imagelst & ";" & openDialog.SelectedItems(i)
    Next
End If
like image 192
Fionnuala Avatar answered Oct 05 '22 12:10

Fionnuala


I needed to select a single text file... this is what I did... it worked fine.

' Get the File
'----------------------------------------------------------
Dim dialog As Object
Dim pickedfile As Boolean
Dim myfile As String
Set dialog = Application.FileDialog(msoFileDialogFilePicker)
With dialog
    .AllowMultiSelect = False
    .Title = "Please pick the file to convert."
    .Filters.Clear
    .Filters.Add "Text Files", "*.TXT"
    .Filters.Add "All Files", "*.*"
    pickedfile = False
    pickedfile = .Show
    If pickedfile Then
    myfile = .SelectedItems.Item(1)
    End If
End With
'----------------------------------------------------------

Additionally... you can replace the dialog type with...

Set dialog = Application.FileDialog(msoFileDialogOpen)

and it worked equally well.

like image 26
Askjerry Avatar answered Oct 05 '22 13:10

Askjerry