GetOpenFileName Function

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

Platforms: Win 32s, Win 95/98, Win NT

GetOpenFileName opens the standard Windows 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 does not actually open the files. The function returns 0 if an error occured or if the user hit Cancel, or returns 1 if successful.

pOPENFILENAME
Holds the parameters needed to open the dialog box. Also receives the returned filename(s) and other information about the user's settings.

Example:

' Call the Open File dialog box and look for *.txt files
Dim filebox As OPENFILENAME  ' structure that sets the dialog box
Dim fname As String  ' will receive selected file's name
Dim retval As Long  ' return value

' Configure how the dialog box will look
filebox.lStructSize = Len(filebox)  ' the size of the structure
filebox.hwndOwner = Form1.hWnd  ' handle of the form calling the function
filebox.lpstrTitle = "Open File"  ' text displayed in the box's title bar
' The next line sets up the file types drop-box
filebox.lpstrFilter = "Text Files" & vbNullChar & "*.txt" & vbNullChar & "All Files" & vbNullChar & "*.*" & vbNullChar & vbNullChar
filebox.lpstrFile = Space(255)  ' initalize buffer that receives path and filename of file
filebox.nMaxFile = 255  ' length of file and pathname buffer
filebox.lpstrFileTitle = Space(255)  ' initialize buffer that receives filename of file
filebox.nMaxFileTitle = 255  ' length of filename buffer
' Allow only existing files and hide the read-only check box
filebox.flags = OFN_PATHMUSTEXIST Or OFN_FILEMUSTEXIST Or OFN_HIDEREADONLY

' Execute the dialog box
retval = GetOpenFileName(filebox)
If retval <> 0 Then  ' if the dialog box completed successfully
  ' Remove null space from the file name
  fname = Left(filebox.lpstrFile, InStr(filebox.lpstrFile, vbNullChar) - 1)
  Debug.Print "The selected file: "; fname
End If

Related Call: GetSaveFileName
Category: Common Dialog
Back to the index.


Go to Paul Kuliniewicz's Home Page.
E-mail: rogue953@hotmail.com
This page is at http://skyscraper.fortunecity.com/transmission/45/api/g/getopenfilename.html