FileOpen - FileSave

The routines to open or save files are very similar. Fill the structure and call the function.

Since opened files are often saved again, both functions and declarations are in a mutual module.

Copy the following code into a second module:

Option Explicit

Public Const strVersion As String = "V1.0"                           'open and save dialogs
Public Const strMakro As String = "Common Dialogs Open/Save"

Sub CATMain()

   CDlgInitialize 0&                                                 'no owner

   If CDlgOpenDlg(0&) Then MsgBox ofn.lpstrFile                      'no owner
   If CDlgSaveDlg(0&, "c:\dummy.txt") Then MsgBox ofn.lpstrFile      'no owner
End Sub

Public Function Trim0(sName As String) As String
   'if there is one or more trailing /0, trim the string
   If InStr(sName, Chr$(0)) Then
      Trim0 = Mid$(sName, 1, InStr(sName, Chr$(0)) - 1)
      Trim0 = sName
   End If
End Function

A parent handle is not needed to call 'FileOpen' or 'FileSave', but may be supplied.

Since the structure needed to the open/save dialogs is very similar, most of the structure is filled in the function 'DlgInitialize' (as in the example in Charles Petzold's book).

The result of a call to 'FileOpen' looks like this (depending on operating system):

 ... and FileSave:

Go to top