FileName

Returns or sets the file name that the Open and Save As dialogs show when they open, and holds the path that the last of those dialogs returned.

Syntax: object.FileName [ = string ]

object
required An object expression that evaluates to a cDialog.
string
A String, a file name or a full path. The default is an empty string.

Remarks

Before a dialog. ShowOpen and ShowSave copy the text into the buffer that Windows uses for the lpstrFile member of OPENFILENAME, which initialises the File name box. The Save As dialog shows an empty box for an empty FileName and x.txt for x.txt. ShowSave pads the buffer with spaces, and the dialog does not show the padding.

After a dialog. A ShowSave or ShowOpen that the user confirms stores the path it returns in FileName. For a MultiSelect selection of several files that is the folder, not a file name. A cancelled dialog leaves FileName as it was, and ShowBrowseForFolder neither reads nor changes it.

Note

Because a confirmed dialog stores the full path, the next ShowOpen or ShowSave passes that path as the initial file name. Set FileName to an empty string, or call Reset, to start without it.

Length. The buffer of ShowSave holds 260 characters. A FileName of up to 259 characters opens the dialog normally. A FileName of exactly 260 characters shows no dialog and returns an empty string at once, without an error, as a cancel does. A FileName of 261 characters or more raises run-time error 5, Invalid procedure call or argument, before any dialog. The buffer of ShowOpen holds 260 characters, or 32,768 when FileName is not empty or MultiSelect is True. A FileName of 32,768 characters or more is not used by ShowOpen.

Example

Dim dlg As New cDialog
dlg.FileName = "C:\Reports\summary.txt"
Debug.Print dlg.FileName                    ' C:\Reports\summary.txt
Debug.Print dlg.GetFileName(dlg.FileName)   ' summary.txt

dlg.Reset
Debug.Print "[" & dlg.FileName & "]"        ' []

This example suggests a name in the Save As dialog and prints the path that the user confirms.

Dim dlg As New cDialog
dlg.FileName = "summary.txt"
If Len(dlg.ShowSave()) > 0 Then
    Debug.Print "Saved as " & dlg.FileName
End If

See Also

  • InitialDir property – the folder in which the dialogs start
  • ShowOpen method – shows the Open dialog
  • ShowSave method – shows the Save As dialog
  • GetFileName method – the name part of a path
  • cDialog class – overview