Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save with msoFileDialogFilePicker

Tags:

excel

vba

I've already searched a lot on that subject and I've tried many things. In fact, I need to ask user to save as a custom type that is not available within the msoFileDialogSaveAs filters.

I know I could save as, let's say, *.txt and then change the extension before save. I did this and it worked but when the user enter the name of the file in a folder already containing other files of the custom type, the user won't see the existing custom files list because the active filter is not of that type.

Therefore, I was wondering if there was a way to use msoFileDialogFilePicker to save as a custom type by typing the name of the file.

Here is what is could look like:

Function userFileSaveDialog_OneFilterOnly(iFilter As String, _
                                    iExtension As String, _
                                    Optional iTitle As String)

    With Application.FileDialog(msoFileDialogFilePicker) '(msoFileDialogSaveAs)
        .Filters.Clear
        .Filters.Add iFilter, iExtension
        .AllowMultiSelect=False
        .ButtonName "Save"
        .Title = iTitle
        If CBool(.Show) Then
            userFileSaveDialog_OneFilterOnly = .SelectedItems(.SelectedItems.Count)
        Else
        End If
    End With

End Function

Thanks for the help!

like image 628
simpLE MAn Avatar asked Nov 28 '13 17:11

simpLE MAn


1 Answers

Like I mentioned in the comments, you can use Application.GetSaveAsFilename

The syntax is

expression.GetSaveAsFilename(InitialFilename, FileFilter, FilterIndex, Title, ButtonText)

And here is an example to use it

Sub Sample()
    Dim Ret

    Ret = userFileSaveDialog_OneFilterOnly("My Special Files", "Sid", "An Example")

    MsgBox Ret
End Sub

Function userFileSaveDialog_OneFilterOnly(iFilter As String, _
                                          iExtension As String, _
                                          Optional iTitle As String)
    Dim Ret

    Ret = Application.GetSaveAsFilename(fileFilter:=iFilter & _
                                                    " (*." & _
                                                    iExtension & _
                                                    "), *." & iExtension, _
                                                    Title:=iTitle)

    If Ret <> False Then userFileSaveDialog_OneFilterOnly = Ret
End Function

In Action

enter image description here

like image 193
Siddharth Rout Avatar answered Sep 26 '22 07:09

Siddharth Rout