Skip to main content

Save File With File Dialog In VBA | Access

A couple of years ago, I posted code for selecting a file with VBA. This example demonstrates how to use the file dialog to return a Save-As string.

VBA File Dialog Save-As


'--------------------------------------------------
' File Dialog Save-As
'--------------------------------------------------
'NOTE: To use this code, you must reference
'The Microsoft Office 14.0 (or current version)
'Object Library by clicking menu Tools>References
'Check the box for:
'Microsoft Office 14.0 Object Library in Access 2010
'Microsoft Office 15.0 Object Library in Access 2013
'Click OK
'--------------------------------------------------
Public Function saveFileAs() As String
    Dim fd As FileDialog, fileName As String
    
    On Error GoTo ErrorHandler
    
    Set fd = Application.FileDialog(msoFileDialogSaveAs)
 
    If fd.Show = True Then
        If fd.SelectedItems(1) <> vbNullString Then
            fileName = fd.SelectedItems(1)
        End If
    Else
        'Stop Code Execution for Null File String
        End
    End If
 
    saveFileAs = fileName
 
    'Cleanup
    Set fd = Nothing
 
    Exit Function
 
ErrorHandler:
    Set fd = Nothing
    MsgBox "Error " & Err & ": " & Error(Err)
 
End Function

Comments

  1. Carl Sansfaçon

    Thanks !!

  2. Leandro Barbosa

    Excelent! Thank you!

Leave a Reply

Your email address will not be published. Required fields are marked *