GetOpenFileName Function

Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (lpofn As OPENFILENAME) As Long

Platforms

Description & Usage

GetOpenFileName opens the Open File common dialog dialog box. All of the settings for creating the dialog, as well as the data returned from it, are placed into the structure passed as lpofn. Note that this function does not actually open any file; it merely prompts the user for a file or files to open, and those filenames are given to your program.

Return Value

If the user selects one or more files in the dialog box, the function returns a nonzero value. If an error occured, or if the user merely clicked Cancel, the function returns zero. Use CommDlgExtendedError to get the error code.

Visual Basic-Specific Issues

None.

Parameters

lpofn
The parameters used to open the dialog box. Also receives the returned filename(s) and other information about what the user selected.

Example

Use the Open File common dialog box to prompt the user for a *.txt file. The user's selection is then displayed (the filename, not the file itself). The dialog box opens when the user clicks button Command1. So, to use this example, place a command button named Command1 on a form window.

' This code is licensed according to the terms and conditions listed here.

' Declarations and such needed for the example:
' (Copy them to the (declarations) section of a module.)
Public Type OPENFILENAME
	lStructSize As Long
	hwndOwner As Long
	hInstance As Long
	lpstrFilter As String
	lpstrCustomFilter As String
	nMaxCustomFilter As Long
	nFilterIndex As Long
	lpstrFile As String
	nMaxFile As Long
	lpstrFileTitle As String
	nMaxFileTitle As Long
	lpstrInitialDir As String
	lpstrTitle As String
	flags As Long
	nFileOffset As Integer
	nFileExtension As Integer
	lpstrDefExt As String
	lCustData As Long
	lpfnHook As Long
	lpTemplateName As String
End Type
Public Const OFN_FILEMUSTEXIST = &H1000
Public Const OFN_HIDEREADONLY = &H4
Public Const OFN_PATHMUSTEXIST = &H800
Public Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (lpofn _
	As OPENFILENAME) As Long

' *** Place the following code inside a form window.

Private Sub Command1_Click()
	Dim filebox As OPENFILENAME  ' open file dialog structure
	Dim fname As String          ' filename the user selected
	Dim result As Long           ' result of opening the dialog
	
	' Configure how the dialog box will look
	With filebox
		' Size of the structure.
		.lStructSize = Len(filebox)
		' Handle to window opening the dialog.
       		.hwndOwner = Me.hWnd
		' Handle to calling instance (not needed).
		.hInstance = 0
		' File filters to make available: Text Files and All Files
		.lpstrFilter = "Text Files (*.txt)" & vbNullChar & "*.txt" & vbNullChar & _
			"All Files (*.*)" & vbNullChar & "*.*" & vbNullChar & vbNullChar
		'.lpstrCustomFilter is ignored -- unused string
		.nMaxCustomFilter = 0
		' Default filter is the first one (Text Files, in this case).
		.nFilterIndex = 1
		' No default filename.  Also make room for received
		' path and filename of the user's selection.
		.lpstrFile = Space(256) & vbNullChar
		.nMaxFile = Len(.lpstrFile)
		' Make room for filename of the user's selection.
		.lpstrFileTitle = Space(256) & vbNullChar
		.nMaxFileTitle = Len(.lpstrFileTitle)
		' Initial directory is C:\.
		.lpstrInitialDir = "C:\" & vbNullChar
		' Title of file dialog.
		.lpstrTitle = "Select a File" & vbNullChar
		' The path and file must exist; hide the read-only box.
		.flags = OFN_PATHMUSTEXIST Or OFN_FILEMUSTEXIST Or OFN_HIDEREADONLY
		' The rest of the options aren't needed.
		.nFileOffset = 0
		.nFileExtension = 0
		'.lpstrDefExt is ignored -- unused string
		.lCustData = 0
		.lpfnHook = 0
		'.lpTemplateName is ignored -- unused string
	End With
	
	' Display the dialog box.
	result = GetOpenFileName(filebox)
	If result <> 0 Then 
		' Remove null space from the file name.
		fname = Left(filebox.lpstrFile, InStr(filebox.lpstrFile, vbNullChar) - 1)
		Debug.Print "The selected file: "; fname
	End If
End Sub

See Also

GetSaveFileName

Category

Common Dialog

Go back to the Function listing.
Go back to the Reference section index.


Last Modified: September 24, 2000
This page is copyright © 2000 Paul Kuliniewicz. Copyright Information.
Go back to the Windows API Guide home page.
E-mail: vbapi@vbapi.com Send Encrypted E-Mail
This page is at http://www.vbapi.com/ref/g/getopenfilename.html