Declare Function GetSaveFileName Lib "comdlg32.dll" Alias "GetSaveFileNameA" (pOPENFILENAME As OPENFILENAME) As Long
GetSaveFileName opens the standard Windows 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 does not actually save the file. The function returns 0 if an error occured or if the user hit Cancel, and returns 1 if successful.
Example:
' Call the Save File dialog box and extract the filename
Dim filebox As OPENFILENAME ' passes data to and from the function
Dim fname As String ' receives path and filename of selected file
Dim retval As Long ' return value
filebox.lStructSize = Len(filebox) ' size of the structure
filebox.hwndOwner = Form1.hWnd ' handle of the window opening the box
filebox.lpstrTitle = "Save File" ' text to display in the title bar
' Set the File Type drop-box values to Text Files and All Files
filebox.lpstrFilter = "Text Files" & vbNullChar & "*.txt" & vbNullChar & "All Files" & vbNullChar & "*.*" & vbNullChar & vbNullChar
filebox.lpstrFile = Space(255) ' receives path and filename of selected file
filebox.nMaxFile = 255 ' size of the path and filename buffer
filebox.lpstrFileTitle = Space(255) ' receives filename of selected file
filebox.nMaxFileTitle = 255 ' size of the filename buffer
filebox.lpstrDefExt = "txt" ' default file extension
' Allow only existing paths, warn if file already exists, hide read-only box
filebox.flags = OFN_PATHMUSTEXIST Or OFN_OVERWRITEPROMPT Or OFN_HIDEREADONLY
retval = GetSaveFileName(filebox) ' open the Save File dialog box
If retval <> 0 Then ' the user chose a file
' Extract the filename from the buffer and put it into fname
fname = Left(filebox.lpstrFile, InStr(filebox.lpstrFile, vbNullChar) - 1)
End If
Related Call: GetOpenFileName
Category: Common Dialog
Back to the index.