API Tip

GetOpenFileName API Function       

           

      For this week's API Tip, we will look at one of the common dialogs You can access through Visual Basic using API functions. Of course, it's not necessary to use API since Microsoft provides ActiveX control which will load common dialogs for You. However, few common dialogs cannot be accessed using the provided control, and also, using API doesn't require ComCtl32.ocx file. That reduces installation package space

GetOpenFileName is used to load Open  File dialog box. Using the function, You can allow the user to select the file name of their choice. Then, the function will return the path and filename of the file the user selected. The declaration of GetOpenFileName:

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

All this code above has to be declared in one line. OPENFILENAME structure looks like this:

Public Type OPENFILENAME
   lStructSize As Long
   hwndOwner As Long
   hInstance As Long
   lpstrFilter As String
   lpstrCustomFilter As String
   nMaxCustFilter 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

Don't get scared because of the size of this thing. In the example, You'll see that this function is pretty straightfoward and easy to use. Just copy the whole OPENFILENAME into the module.

 Example: Shows Open File dialog box:

      Private Sub cmdOpen_Click()

           Dim retVal As Long ' return value of the function
           Dim opfS As OPENFILENAME ' variable to hold the structure

           Const MAX_BUFFER = 255 ' the maximum length of the file name

           With opfS ' populate the structure
                 .lStructSize = Len(opfS) ' the size of the structure
                 .hwndOwner =Form1.hWnd ' the owner of the dialog (the form handle)
                 .hInstance = App.hInstance ' instance of the app, use App.hInstance
                 .lpstrTitle = "Select Back Up File..." ' the title of the dialog box
                 .lpstrInitialDir ="c:\"  ' initial directory which the user will see
                 .lpstrFilter = "All Files" & Chr$(0) & "*.*" & Chr$(0) ' what files to show
                 .nFilterIndex = 1 '
                 .lpstrFile = String(MAX_BUFFER+1, 0) ' initialize empty string to get 'filename
                 .nMaxFile = MAX_BUFFER ' the maximum length of the filename
                 .lpstrFileTitle = .lpstrFile '
                 .nMaxFileTitle = MAX_BUFFER
           End With

           retVal = GetOpenFileName(opfS)

           If rc Then msgbox opfS.lpstrFile ' show the filename the user selected

End Sub

Once You obtain the path of the file the user selected, You can use it as appropriate (delete it for example with DeleteFile function).

Next week: GetSaveFileName

Api Tips - Home

          

Made By Laimonas Simutis. 2001. laijerrad@yahoo.com