ShowOpen

Shows the Open dialog and returns the file that the user chose.

Syntax: object.ShowOpen ( ) As Variant

object
required An object expression that evaluates to a cDialog.

Returns a Variant that always holds a String. When the user confirms the dialog, it holds the full path of the chosen file. When the user cancels, it holds an empty string. With MultiSelect True and several files selected, it holds only a folder, as below describes.

Remarks

ShowOpen calls GetOpenFileNameW and takes its options from the properties of the object:

Property What ShowOpen passes
DialogTitle lpstrTitle, the title
InitialDir lpstrInitialDir, the starting folder
FileName lpstrFile, the text in the File name box when the dialog opens
Filter lpstrFilter, the file-type list
DefaultExt lpstrDefExt, the extension added to a name typed without one
MultiSelect OFN_ALLOWMULTISELECT
OverwritePrompt OFN_OVERWRITEPROMPT, documented for Save As only
PathMustExist OFN_PATHMUSTEXIST
FileMustExist OFN_FILEMUSTEXIST
HideReadOnly OFN_HIDEREADONLY
ReadOnly OFN_READONLY
NoChangeDir OFN_NOCHANGEDIR
CreatePrompt OFN_CREATEPROMPT

A flag is set when its property is True. The method always sets OFN_LONGNAMES, OFN_EXPLORER and OFN_ENABLESIZING, selects the first pair of the filter, and has no owner window, hook or template. The class page lists these settings.

An empty string means that the user cancelled, or that Windows reported a failure, such as a selected path that does not fit into the buffer, and no value tells which of the two happened. The buffer holds 260 characters, or 32,768 when MultiSelect is True or FileName is not empty. Neither case raises an error.

A confirmed ShowOpen also stores the path it returns in FileName, and leaves InitialDir as it was. A cancelled dialog changes neither. The next call therefore passes the previous path as the initial file name.

Several files

Windows returns the selected names in one buffer, and the class cuts that buffer at its first null character, which ends the folder, before it looks for the separators between the names. The code that splits the names and returns an array of full paths therefore never runs. See MultiSelect for a way to detect the result.

Example

This example shows the Open dialog for text files and prints the chosen path and name.

Dim dlg As New cDialog
Dim path As String

dlg.DialogTitle = "Open a text file"
dlg.InitialDir = "C:\Reports"
dlg.SetCommonFilters

path = dlg.ShowOpen()
If Len(path) = 0 Then
    Debug.Print "Cancelled"
Else
    Debug.Print path
    Debug.Print dlg.GetFileName(path)
End If

See Also