Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (pOPENFILENAME As OPENFILENAME) As Long
GetOpenFileName opens the standard Windows 95 Open File dialog box. All of the information you need to pass to the function to set up the dialog box are passed inside pOPENFILENAME. Also, the filename(s) (if any) returned by the function are also put into pOPENFILENAME. Note that all this function does is run the dialog box and returns the file(s) chosen. It of course does not actually open the files. The function returns 0 if an error occured or if the user hit Cancel, and returns a non-zero value if successful.
Example:
Dim file As OPENFILENAME
' Call the Open File dialog box and read the filename
file.hwndOwner = Form1.hWnd ' Calling form's handle
file.lpstrTitle = "Open File" ' Title bar
' Set the File Type drop-box values
file.lpstrFilter = "Text Files" & vbNullChar & "*.txt" & vbNullChar & vbNullChar
file.lpstrFile = Space(255) ' Path and filename buffer
file.nMaxFile = 255 ' Length of buffer
file.lpstrFileTitle = Space(255) ' Filename buffer
file.nMaxFileTitle = 255 ' Length of buffer
' Only existing files, hide read-only check box
file.flags = OFN_PATHMUSTEXIST Or OFN_FILEMUSTEXIST Or OFN_HIDEREADONLY
file.lStructSize = Len(file) ' This variable's size
x = GetOpenFileName(file)
If x = 0 Then Exit Sub ' Abort on error or Cancel
' Extract the filename
temp = Trim(file.lpstrFile)
filename = Left(temp, Len(temp) - 1) ' Trim ending vbNullChar
Related Call: GetSaveFileName
Category: Common Dialog
Back to the index.