# GetSaveFileName Function

`Declare Function GetSaveFileName Lib "comdlg32.dll" Alias "GetSaveFileNameA" (pOPENFILENAME As OPENFILENAME) As Long`

GetSaveFileName opens the standard Windows 95 Save 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 (if any) returned by the function is also put into pOPENFILENAME. Note that all this function does is run the dialog box and returns the file chosen. It of course does not actually save the file. The function returns 0 if an error occured or if the user hit Cancel, and returns a non-zero value if successful.

pOPENFILENAME
  Holds the parameters needed to open the dialog box. Also receives the returned filename.

Example:

    Dim file As OPENFILENAME
    ' Call the Save File dialog box and read the filename
    file.hwndOwner = Form1.hWnd  ' Calling form's handle
    file.lpstrTitle = "Save File"  ' Title bar
    ' Set the File Type drop-box values
    file.lpstrFilter = "Text Files" & vbNullChar & "*.txt" & vbNullChar & vbNullChar
    file.lpstrFile = Space(255)  ' Path and filename buffer
    file.nMaxFile = 255  ' Length of buffer
    file.lpstrFileTitle = Space(255)  ' Filename buffer
    file.nMaxFileTitle = 255  ' Length of buffer
    file.lpstrDefExt = "txt"  ' Default file extension
    ' Only existing paths, warn if already exists, hide read-only box
    file.flags = OFN_OVERWRITEPROMPT Or OFN_PATHMUSTEXIST Or OFN_HIDEREADONLY
    file.lStructSize = Len(file)  ' This variable's size
    x = GetSaveFileName(file)
    If x = 0 Then Exit Sub  ' Abort on error or Cancel
    ' Extract the filename
    temp = Trim(file.lpstrFile)
    filename = Left(temp, Len(temp) - 1)  ' Trim ending vbNullChar

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