Visual Basic Common Dialog Routines
GetSaveFileName: File Save Common Dialog Basics
     
Posted:   Friday December 27, 1996
Updated:   Monday December 26, 2011
     
Applies to:   VB4-32, VB5, VB6
Developed with:   VB4-32, Windows 95
Revised with:   VB4, Windows NT4
OS restrictions:   None
Author:   VBnet - Randy Birch
     
Related:   GetOpenFileName: File Open/Save Common Dialog API - Overview
GetOpenFileName: File Open Common Dialog Basics
GetOpenFileName: File Dialog Centering using a Callback
GetOpenFileName: Advanced Dialog Centering using a Callback
GetOpenFileName: Customize File Open/Save Common Dialog Controls
     
 Prerequisites
None.

This page provides the code required for the Common Dialog GetSaveFileName API method. OPENFILENAME member descriptions, as well as commented code, are covered in the pages above. Except for the Flags and API name, the code is pretty well a duplicate of the GetOpenFileName code.

This demo uses the same form created in GetOpenFileName: File Open Common Dialog Basics -- unneeded controls have been hidden.

For a discussion on modifying the OPENFILENAME structure in order to display the new Windows 2000 or Windows XP-style Open and Save dialogs, please see the comments section in the page GetOpenFileName: File Dialog Centering using a Callback (note: a hook is not required to display this new-style dialog - it just so happens that page is the one I chose to explain this on.)

 BAS Module Code
None.

 Form Code
To the form created in GetOpenFileName: File Open Common Dialog Basics, add a second command button (Command2), along with the following code:

Option Explicit
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Copyright 1996-2011 VBnet/Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Distribution: You can freely use this code in your own
'               applications, but you may not reproduce 
'               or publish this code on any web site,
'               online service, or distribute as source 
'               on any media without express permission.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Sub Command2_Click()

  'used in call setup
   Dim sFilters As String
   
  'used after call
   Dim buff As String
   Dim sLname As String
   Dim sSname As String
   Dim pos as Long

  'create string of filters for the dialog
   sFilters = "Visual Basic Forms" & vbNullChar & _
              "*.frm" & vbNullChar & _
              "Visual Basic Modules" & vbNullChar & _
              "*.bas" & vbNullChar & _
              "Visual Basic Projects" & vbNullChar & _
              "*.vbp" & vbNullChar & _
              "Text Files" & vbNullChar & _
              "*.txt" & vbNullChar & _
              "All Files" & vbNullChar & _
              "*.*" & vbNullChar & vbNullChar
 
 
   With OFN
   
      .nStructSize = Len(OFN)
      .hWndOwner = Form1.hWnd
      .sFilter = sFilters
      .nFilterIndex = 2
      .sFile = "Untitled.bas" & Space$(1024) & _
               vbNullChar & vbNullChar
      .nMaxFile = Len(.sFile)
      .sDefFileExt = "bas" & vbNullChar & vbNullChar
      .sFileTitle = vbNullChar & Space$(512) & _
                    vbNullChar & vbNullChar
      .nMaxTitle = Len(OFN.sFileTitle)
      .sInitialDir = "d:\vb5" & vbNullChar & vbNullChar
      .sDialogTitle = "VBnet GetSaveFileName Demo"
      .flags = OFS_FILE_SAVE_FLAGS

   End With
   
   
  'call the API
   If GetSaveFileName(OFN) Then
    
     'see illustration for descriptions
      Text1.Text = OFN.sFile
      Text2.Text = Left$(OFN.sFile, OFN.nFileOffset)
      Text3.Text = Mid$(OFN.sFile, OFN.nFileOffset + 1, _
                   Len(OFN.sFile) - OFN.nFileOffset - 1)
      Text4.Text = Mid$(OFN.sFile, OFN.nFileExtension + 1, _
                   Len(OFN.sFile) - OFN.nFileExtension)
      Text5.Text = OFN.sFileTitle
      
      sLname = OFN.sFileTitle
      sSname = Space$(128)
      pos = GetShortPathName(sLname, sSname, Len(sSname))
      Text6.Text = LCase$(Left$(sSname, pos))
     
      sLname = OFN.sFile
      sSname = Space$(128)
      pos = GetShortPathName(sLname, sSname, Len(sSname))
      Text7.Text = LCase$(Left$(sSname, pos))
  
  End If
  
End Sub
 Comments
Once the displayed dialog has closed, the file details for the selected or new file will be returned on the form.

For a discussion on modifying the OPENFILENAME structure in order to display the new Windows 2000 or Windows XP-style Open and Save dialogs, please see the comments section in the page GetOpenFileName: File Dialog Centering using a Callback (note: a hook is not required to display this new-style dialog - it just so happens that page is the one I chose to explain this on.)


 
 

PayPal Link
Make payments with PayPal - it's fast, free and secure!

 
 
 
 

Copyright 1996-2011 VBnet and Randy Birch. All Rights Reserved.
Terms of Use  |  Your Privacy

 

Hit Counter