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. When the user confirms the dialog, it holds the full path of the chosen file as a String. When the user cancels, it holds an empty string. With MultiSelect True and two or more files selected, it holds a 0-based array of String values, one full path for each file, 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 that returns a String also stores it 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
With MultiSelect True, Windows returns the folder and then the name of each selected file in one buffer. ShowOpen joins the folder to each name with a backslash and returns the full paths as an array of String values, in the order in which the names stand in the File name box, and sets FileName to an empty string. One selected file gives its full path as a String, as without MultiSelect. Test the result with IsArray, or call SelectFiles, which returns a Collection in both cases.
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
- ShowSave method – shows the Save As dialog
- ShowBrowseForFolder method – shows the Browse For Folder dialog
- SelectAndOpenFile method – shows this dialog and opens the file
- SelectFiles method – shows this dialog and returns the files as a collection
- MultiSelect property – lets the user select several files
- FileName property – holds the path afterwards
- cDialog class – overview