Save As Dialog Box (1 Viewer)

kdog436089

New member
Local time
Today, 06:26
Joined
Oct 16, 2014
Messages
6
Hello Forum,

I have another VBA Question, possibly been answered many times before. I wish to export a report to excel, but I wish to prompt the user to be able to choose where to save the file at. How can I have the "Save As" dialog box pop up, with the filter set to "*.xls"?

Thanks in advance.
 

Rx_

Nothing In Moderation
Local time
Today, 07:26
Joined
Oct 22, 2009
Messages
2,803
Maybe something like this?
Note: The msoFileDialogOpen and msoFileDialogSaveAs constants are not supported in Microsoft Access.
You can probably set your own filter here.
Code:
Private Sub cmdFileDialog_Click()
 
   ' Requires reference to Microsoft Office 11.0 Object Library (or your version)
 
   Dim fDialog As Office.FileDialog
   Dim varFile As Variant
 
   ' Clear listbox contents.
   Me.FileList.RowSource = ""
 
   ' Set up the File Dialog.
   Set fDialog = Application.[B]FileDialog(msoFileDialogFilePicker)[/B]
 
   With fDialog
 
      ' Allow user to make multiple selections in dialog box
      .AllowMultiSelect = True
 
      ' Set the title of the dialog box.
      .Title = "Please select one or more files"
 
      ' Clear out the current filters, and add our own.
      .Filters.Clear
      .Filters.Add "Access Databases", "*.MDB"
      .Filters.Add "Access Projects", "*.ADP"
      .Filters.Add "All Files", "*.*"
 
      ' Show the dialog box. If the .Show method returns True, the
      ' user picked at least one file. If the .Show method returns
      ' False, the user clicked Cancel.
      If .Show = True Then
 
         'Loop through each file selected and add it to our list box.
         For Each varFile In .SelectedItems
            Me.FileList.AddItem varFile
         Next
 
      Else
         MsgBox "You clicked Cancel in the file dialog box."
      End If
   End With
End Sub
 

Rx_

Nothing In Moderation
Local time
Today, 07:26
Joined
Oct 22, 2009
Messages
2,803
Just in case it is Access 2010, I just realized this after searching.
Since I jumped from Access 2000, 2003 to Access 2010, I never looked for it! May have to revisit some code.

So, according to MSAccess, developers *can* use FileDialog to display a Save As dialog in Access 2010 (just not in earlier versions).
Important, it is limited as it cannot clear or add filters, however. The "Save as type" dropdown contains "All files (*.*)" as only option.

Sample code:
Code:
Sub TestFileDialog()
Dim strFilename As String
 With Application.FileDialog(msoFileDialogSaveAs)
   If .Show Then
   strFilename = .SelectedItems(1)
  Else
    MsgBox "No filename specified!", vbExclamation
Exit Sub
End If
End With
' Do something dreadful with strFilename
...
End Sub
 

Users who are viewing this thread

Top Bottom