Propriété FileDialog.InitialFileName (Office)

Définit ou renvoie une valeur String représentant le chemin d’accès ou le nom de fichier qui s’affiche au départ dans une boîte de dialogue d’accès aux fichiers. Lecture-écriture.

Syntaxe

expression.InitialFileName

expression Variable qui représente un objet FileDialog.

Remarques

Utilisez les * caractères génériques et ? lors de la spécification du nom de fichier, mais pas lors de la spécification du chemin d’accès. Le symbole* représente un nombre quelconque de caractères consécutifs et ? représente un caractère unique. Par exemple, . InitialFileName = "c:\c*s.txt" renvoie « cartes.txt » et « continentales.txt ».

Si vous spécifiez un chemin d'accès et aucun nom de fichier, tous les fichiers autorisés par le filtre de fichiers apparaissent dans la boîte de dialogue.

Si vous spécifiez un fichier qui existe dans le dossier initial, ce fichier est le seul qui s’affiche dans la boîte de dialogue.

Si vous spécifiez un nom de fichier qui n’existe pas dans le dossier initial, la boîte de dialogue ne contient alors aucun fichier. Le type de fichier que vous spécifiez dans la propriété InitialFileName remplace les paramètres de filtre de fichier.

Si vous spécifiez un chemin d’accès non valide, le chemin d’accès utilisé en dernier est utilisé. Un message avertit les utilisateurs lorsqu'un chemin non valide est utilisé.

Si vous affectez à cette propriété une chaîne supérieure à 256 caractères, une erreur d'exécution se produit.

Exemple

L’exemple suivant affiche une boîte de dialogue File Picker avec l’objet FileDialog et affiche chaque fichier sélectionné dans une zone de message.

Sub Main() 
 
 'Declare a variable as a FileDialog object 
 Dim fd As FileDialog 
 
 'Create a FileDialog object as a File Picker dialog box. 
 Set fd = Application.FileDialog(msoFileDialogFilePicker) 
 
 'Declare a variable to contain the path 
 'of each selected item. Even though the path is aString, 
 'the variable must be a Variant because For Each...Next 
 'routines only work with Variants and Objects. 
 Dim vrtSelectedItem As Variant 
 
 'Use a With...End With block to reference the FileDialog object. 
 With fd 
 
 'Set the initial path to the C:\ drive. 
 .InitialFileName = "C:\" 
 
 'Use the Show method to display the File Picker dialog box and return the user's action. 
 'If the user presses the button... 
 If .Show = -1 Then 
 
 'Step through each string in the FileDialogSelectedItems collection. 
 For Each vrtSelectedItem In .SelectedItems 
 
 'vrtSelectedItem is aString that contains the path of each selected item. 
 'Use any file I/O functions that you want to work with this path. 
 'This example displays the path in a message box. 
 MsgBox "Selected item's path: " & vrtSelectedItem 
 
 Next vrtSelectedItem 
 'If the user presses Cancel... 
 Else 
 End If 
 End With 
 
 'Set the object variable to Nothing. 
 Set fd = Nothing 
 
End Sub 

Voir aussi

Assistance et commentaires

Avez-vous des questions ou des commentaires sur Office VBA ou sur cette documentation ? Consultez la rubrique concernant l’assistance pour Office VBA et l’envoi de commentaires afin d’obtenir des instructions pour recevoir une assistance et envoyer vos commentaires.