Prompt SaveAs dialog when exporting data

Snowflake68

Registered User.
Local time
Today, 02:24
Joined
May 28, 2014
Messages
464
I have found this code on the web that will prompt the SaveAs dialog box when exporting a file but how can I modify it so that it populates the filename with a specific filename such as "Results" plus the current date?

Code:
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

I am using the above code in the transfer spreadsheet code below.

Code:
Private Sub cmdExcelExport_Click()
    
    DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, "qryData", saveFileAs, True

End Sub
 
Last edited:
add that to FileDialog InitialFileName, eg:

Set fd = Application.FileDialog(msoFileDialogSaveAs)
fd.InitialFileName = "Results_" & Date
 
Adding the date as it is will cause an error due to the / present in the date format. You'll need to format it something like

Code:
Set fd = Application.FileDialog(msoFileDialogSaveAs)
fd.InitialFileName = "Results_" & Format(Date,"yyyymmdd")
 
had to format the date though as wouldnt allow slashes in file names but all good now.
Code:
fd.InitialFileName = "SupplyChainMonitor_" & Format(Date, "yyyymmdd")

thanks again.
 
Adding the date as it is will cause an error due to the / present in the date format. You'll need to format it something like

Code:
Set fd = Application.FileDialog(msoFileDialogSaveAs)
fd.InitialFileName = "Results_" & Format(Date,"yyyymmdd")

Thanks I discovered that too and managed to sort it :p
 
The excel file, that is what we are putting the cf
 

Users who are viewing this thread

Back
Top Bottom