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
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()
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
Made By Laimonas Simutis. 2001. firstname.lastname@example.org