How to make msoFileDialogSaveAs pre select file format

dsbrin

New Member
Joined
Jul 7, 2017
Messages
7
Hey all,

Long time lurker posting for the first time.

I'm having a real hard time trying to make msoFileDialogSaveAs to pre select the file format of the Save As dialog based on the current file format. Basically, if the current file is .xlsm, I would like that the dialog pre selected the .xlsm format or whatever the format of the current file, .csv, .xlsx, etc.

As you can note in the following image, my code is not working properly: http://i.imgur.com/8LSyTWW.png (please note x.xlsm on the top of the screen vs the .xlsx in the file type selector).

The sub:

Set dlgFile = Application.FileDialog(msoFileDialogSaveAs)

dlgFile.title = "Neat Addin"

With dlgFile
.InitialFileName = CurrentDate & "_" & FName & "_v1.1_" & MyInit

If .Show = -1 Then
myFileName = .SelectedItems(1)
End If
End With

Set dlgFile = Nothing

'Name checking ends
End If

'Save file
ActiveWorkbook.SaveAs Filename:=myFileName, FileFormat:=ActiveWorkbook.FileFormat


This is only part of the code. All the variables are being called and the sub works perfectly if the file I'm saving is already in xlsx.

Any thoughts?

Thanks a lot. Best,
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Code:
Sub testSaveAs()
    Dim dlgFile As FileDialog
    Dim i As Integer
    Dim ext As String
    Dim fileName As String
    
    Set dlgFile = Application.FileDialog(msoFileDialogSaveAs)
    ext = "*.xlsm"
    fileName = "MyFileNameHere"
    For i = 1 To dlgFile.Filters.Count
        If dlgFile.Filters(i).Extensions = "*.xlsm" Then
            dlgFile.FilterIndex = i
            Exit For
        End If
    Next
    With dlgFile
        .InitialFileName = fileName
        .Show
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,054
Messages
6,176,109
Members
452,707
Latest member
Cruzito

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top