GetSaveFileName 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!
GetSaveFileName is used to load Save File dialog box. Using the function, You can allow the user to select the location and the name of the file which they want to save. Then, the function will return the path and filename of the file the user chose. The declaration of GetSaveFileName:
Public Declare Function GetSaveFileName Lib "comdlg32.dll" Alias
"GetSaveFileNameA" (pOpenfilename As OPENFILENAME) As Long
All this code above has to be declared in one line. OPENFILENAME structure looks like this:
Public Type OPENFILENAME
That's the same structure you use to call GetOpenFileName function. 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 Save File dialog box:
Private Sub cmdOpen_Click()
Once You obtain the path of the file the user selected, You can use it to save data on the hard disk.
Next bonus TIP!!!: How present a dialog for selecting a folder using just Win API!!!
Made By Laimonas Simutis. 2001. email@example.com