Comment puis-je obtenir un nom de fichier d'un objet de dialogue de fichier dans VBA (pour MS Access 2007)?

StackOverflow https://stackoverflow.com/questions/2158675

  •  23-09-2019
  •  | 
  •  

Question

Comment puis-je modifier mon code pour obtenir le nom de fichier au lieu du nom du répertoire? openDialog.InitialFilename me donne le nom du répertoire.
openDialog.FileName me donne l'erreur « Méthode ou membre de données non trouvée ».

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
Était-ce utile?

La solution

Vous voulez:

OpenDialog.SelectedItems.Item(1)

Au lieu de:

OpenDialog.InitialFileName

Comme vous ne l'avez pas permis multiselect.


''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

Si AllowMultiSelect est utilisé, vous devez itérer 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

Autres conseils

Je avais besoin de sélectionner un fichier texte ... ce que je l'ai fait ... il a bien fonctionné.

' 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
'----------------------------------------------------------

De plus ... vous pouvez remplacer le type de dialogue avec ...

Set dialog = Application.FileDialog(msoFileDialogOpen)

et cela a fonctionné aussi bien.

Private Sub Command135_Click()

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

Me.Form.Picture = myfile
End Sub


Command_135=Button Name
Me.Form.Picture = "The Control Name"
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top